itertools

Itertools without repetition but with multiple combinations

Itertools without repetition but with multiple combinations Question: i can’t seem to find a way to use itertools without repetitions while keeping multiple combinations. What I want : > my_list = [‘a’, ‘b’, ‘c’] > the_result_i_want = [‘ab’, ‘ac’, ‘ba’, ‘bc’, ‘ca’, ‘cb’] What I manage to do so far : for i in range …

Total answers: 1

iterating over dynamically augmented iterator

iterating over dynamically augmented iterator Question: I am iterating over lines of a file, but if a line matches a certain pattern, I first need to recursively iterate over another file. I’m using itertools.chain to extend the iterator. The problem is (probably) that the for loop in the __iter__ method makes a copy of the …

Total answers: 1

Find all permutations of a string that is variable only at specific positions in Python

Find all permutations of a string that is variable only at specific positions in Python Question: I have a DNA sequence which is variable only at specific locations and need to find all possible scenarios: DNA_seq=’ANGK’ #N can be T or C and K can be A or G N=[‘T’,’C’] K=[‘A’,’G’] Results: [‘ATGA’,’ATGG’,’ACGA’,’ACGG’] Searching online, …

Total answers: 1

Yet another combinations with conditions question

Yet another combinations with conditions question Question: I want to efficiently generate pairs of elements from two lists equal to their Cartesian product with some elements omitted. The elements in each list are unique. The code below does exactly what’s needed but I’m looking to optimize it perhaps by replacing the loop. See the comments …

Total answers: 4

Creating permutations of tree diagram dynamically and scalable

Creating permutations of tree diagram dynamically and scalable Question: I am trying to create permutations which follows a datatree design in Python. The root should be dynamically and could contain a list of lists. This image shows how the permutations should be created Arrows show allowed combinations. This image explains what could be combined in …

Total answers: 1

Regrouping a list of dictionary

Regrouping a list of dictionary Question: I have a list of dictionary from a database query. I want to group the data by "id" and also add some new key-value pairs based on some existing data in the dictionary. Here is the data: data = [{‘id’: 1, ‘name’: ‘The Musical Hop’, ‘city’: ‘San Francisco’, ‘state’: …

Total answers: 1

Create all possible combinations of lists of different sizes in numpy

Create all possible combinations of lists of different sizes in numpy Question: I want to create a numpy array with all possible combinations of items from multiple lists of different sizes: a = [1, 2] b = [3, 4] c = [5, 6, 7] d = [8, 9, 10] In each combination, I want 2 …

Total answers: 3