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 = ...
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) ] #...
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?
...
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...
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] ...
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...
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))...
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,...
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...