itertools

In python: how to apply itertools.product to elements of a list of lists

I have a list of arrays and I would like to get the cartesian product of the elements in the arrays. I will use an example to make this more concrete... itertools.product seems to do the trick but I am stuck in a little detail. arrays = [(-1,+1), (-2,+2), (-3,+3)]; If I do cp = list(itertools.product(arrays)); I get cp = cp0 = ...

What should itertools.product() yield when supplied an empty list?

I guess it's an academic question, but the second result does not make sense to me. Shouldn't it be as thoroughly empty as the first? What is the rationale for this behavior? from itertools import product one_empty = [ [1,2], [] ] all_empty = [] print [ t for t in product(*one_empty) ] # [] print [ t for t in product(*all_empty) ] #...

Does python have a non-lazy version of itertools.groupby?

I don't need the laziness of itertools.groupby. I just want to group my list into a dict of lists as such: dict([(a, list(b)) for a,b in itertools.groupby(mylist, mykeyfunc)]) Is there a standard function that already does this? ...

How to use itertools.groupby when the key value is in the elements of the iterable?

To illustrate, I start with a list of 2-tuples: import itertools import operator raw = [(1, "one"), (2, "two"), (1, "one"), (3, "three"), (2, "two")] for key, grp in itertools.groupby(raw, key=lambda item: item[0]): print key, list(grp).pop()[1] yields: 1 one 2 two 1 one 3 three 2 two In an attempt...

Special type of combination using itertools

I am almost finished with a task someone gave me that at first involved easy use of the product() function from itertools. However, the person asked that it should also do something a bit different like: li = [[1, 2, 3], [4, 5, 6]] A regular product() would give something like: [1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4] ...

How do I yield a pre-unpacked list?

I have a list that is created within an itertools.groupby operation: def yield_unpacked_list(): for key, grp in itertools.groupby(something_to_groupby, key=lambda x: x[0]): subset_of_grp = list(item[2] for item in list(grp)) yield key, subset_of_grp If, for example, subset_of_grp turns out to be [1, 2, 3, 4] and [5...

Python - Speed up generation of permutations of a list (and process of checking if permuations in Dict)

I need a faster way to generate all permutations of a list, then check if each one is in a dictionary. for x in range (max_combo_len, 0, -1): possible_combos = [] permutations = list(itertools.permutations(bag,x)) for item in permutations: possible_combos.append(" ".join(item))...

Pythonic way of copying an iterable object

For a small project I'm working on I need to cycle through a list. For each element of this cycle I have to start another cycle through the same list, with the former element as first element of the new cycle. For example I'd like to be able to produce something like this: 1, 2, 3, 4, 1, 2, 3, 4, 1, ... 2, 3, 4, 1, 2, 3, 4, 1, 2, ... 3,...

Generate a list of length n with m possible elements

Hi there, I need to generate a ton of lists in Python. Every list is of length 13, and I have 4 possible values that can go into each element. These are [1, -1, i, -i], but it could be whatever. Thus I should get 4 * 4 * 4 ... * 4 = 4^13 = 67,108,864 lists, or more generally, m^n, given the info in the subject. I tried the combination...