combinatorics

Create cartesian product of strings by replacing substrings from a list

Create cartesian product of strings by replacing substrings from a list Question: I have a dictionary with placeholders and their possible list of values, as shown below: { "~GPE~": [‘UK’, ‘USA’], "~PERSON~": [‘John Davies’, ‘Tom Banton’, ‘Joe Morgan’], # and so on … } I want to create all possible combinations of strings by replacing …

Total answers: 1

Combinations with Replacement and maximal Occurrence Constraint

Combinations with Replacement and maximal Occurrence Constraint Question: from itertools import * import collections for i in combinations_with_replacement([‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’a’,’b’,’c’,’d’,’e’,’f’],15): b = (”.join(i)) freq = collections.Counter(b) for k in freq: if freq [k] < 5: print(k) this code most print chars what count if less than 5 what i try do , cheek if at string from …

Total answers: 2

python combination with replacement not working as expected

python combination with replacement not working as expected Question: I would like to produce all combinations of certain length by using a set of allowed characters with replacement: I thought itertools.combinations_with_replacement is what I need. So this is what I did: from itertools import combinations_with_replacement allowed = [‘0’, ‘1’, ‘8’, ‘6’, ‘9’] comb = list(combinations_with_replacement(allowed,2)) …

Total answers: 2

Combinations with repetition in python, where order MATTERS

Combinations with repetition in python, where order MATTERS Question: From python’s Documentation: https://docs.python.org/2/library/itertools.html#itertools.combinations see combinations_with_replacement: “# combinations_with_replacement(‘ABC’, 2) –> AA AB AC BB BC CC” I’d like to use the same function, with the bonus of generating “BA”, “CA”, and “CB”. Asked By: Alexander || Source Answers: itertools.product is definitely the method you’re looking for …

Total answers: 1

Every permutation of list elements (without replacement)

Every permutation of list elements (without replacement) Question: In Python 2.7 I’d like to get the self-cartesian product of the elements of a list, but without an element being paired with itself. In[]: foo = [‘a’, ‘b’, ‘c’] In[]: [x for x in itertools.something(foo)] Out[]: [(‘a’, ‘b’), (‘a’, ‘c’), (‘b’, ‘a’), (‘b’, ‘c’), (‘c’, ‘a’), …

Total answers: 1

General bars and stars

General bars and stars Question: I’ve got a the following “bars and stars” algorithm, implemented in Python, which prints out all decomposition of a sum into 3 bins, for sums going from 0 to 5. I’d like to generalise my code so it works with N bins (where N less than the max sum i.e …

Total answers: 9

Generate all permutations of a list without adjacent equal elements

Generate all permutations of a list without adjacent equal elements Question: When we sort a list, like a = [1,2,3,3,2,2,1] sorted(a) => [1, 1, 2, 2, 2, 3, 3] equal elements are always adjacent in the resulting list. How can I achieve the opposite task – shuffle the list so that equal elements are never …

Total answers: 12

List All Combinations (Python)

List All Combinations (Python) Question: I am trying to output all the combinations of a list with certain constraints: I need to print all the combinations of length x with domain 1 to y. For instance, let x=3, and domain y=4. I need to generate all possible combinations of 1 to 4 with 3 members, …

Total answers: 1