itertools

Python itertools.product reorder the generation

强颜欢笑 提交于 2019-12-04 11:45:17
问题 I have this: shape = (2, 4) # arbitrary, could be 3 dimensions such as (3, 5, 7), etc... for i in itertools.product(*(range(x) for x in shape)): print(i) # output: (0, 0) (0, 1) (0, 2) (0, 3) (1, 0) (1, 1) (1, 2) (1, 3) So far, so good, itertools.product advances the rightmost element on every iteration. But now I want to be able to specify the iteration order according to the following: axes = (0, 1) # normal order # output: (0, 0) (0, 1) (0, 2) (0, 3) (1, 0) (1, 1) (1, 2) (1, 3) axes = (1,

Pythonic way of copying an iterable object

折月煮酒 提交于 2019-12-04 07:12:55
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, 4, 1, 2, 3, 4, 1, 2, 3, ... 4, 1, 2, 3, 4, 1, 2, 3, 4, ... 1, 2, 3, 4, 1, 2, 3, 4, 1, ... ... I thought that copying a itertools.cycle after each .next() would conserve the current state, so that I can begin the new cycle with the element from the "outer" cycle

Using itertools for recursive function application

佐手、 提交于 2019-12-04 05:53:26
I need a Python function iterate(f, x) that creates an iterator returning the values x, f(x), f(f(x)), f(f(f(x))), etc (like, e.g., Clojure's iterate ). First of all, I was wondering: Does this already exist somewhere in the standard library and I'm only missing it? Of course it's easy enough to implement with a generator: def iterate(f, x): while True: yield x x = f(x) Just out of curiosity: Is there a more functional way to do this in Python, e.g. with some itertools or functools magic? In Python 3.3 this would work def iterate(f, x): return accumulate(repeat(x), lambda acc, _ : f(acc)) but

Name parts of iterables in itertools.products

十年热恋 提交于 2019-12-04 05:45:44
I've been reading about itertools , which seems to be a very powerful module. I am particularly interested in itertools.product() which appears to give me all of the combinations of the iterable inputs. However, I would like to know which of the input iterables each of the outputs are coming from. For example, a simple standard example is: itertools.product([1, 2, 3], [1, 2]) If the user provided the inputs of [1,2,3], [1, 2] I won't know which order they came in, so getting a result of (1, 2) isn't much help, as I don't know which way round they will be. Is there some way of providing input

How do I reverse an itertools.chain object?

核能气质少年 提交于 2019-12-04 02:50:50
My function creates a chain of generators: def bar(num): import itertools some_sequence = (x*1.5 for x in range(num)) some_other_sequence = (x*2.6 for x in range(num)) chained = itertools.chain(some_sequence, some_other_sequence) return chained My function sometimes needs to return chained in reversed order. Conceptually, the following is what I would like to be able to do: if num < 0: return reversed(chained) return chained Unfortunately: >>> reversed(chained) TypeError: argument to reversed() must be a sequence What are my options? This is in some realtime graphic rendering code so I don't

How to unzip an iterator?

瘦欲@ 提交于 2019-12-04 02:50:01
Given a list of pairs xys , the Python idiom to unzip it into two lists is: xs, ys = zip(*xys) If xys is an iterator, how can I unzip it into two iterators, without storing everything in memory? Ami Tavory Suppose you have some iterable of pairs: a = zip(range(10), range(10)) If I'm correctly interpreting what you are asking for, you could generate independent iterators for the firsts and seconds using itertools.tee : xs, ys = itertools.tee(a) xs, ys = (x[0] for x in xs), (y[1] for y in ys) Note this will keep in memory the "difference" between how much you iterate one of them vs. the other.

My IDLE does not recognize itertools.izip() as a function

大城市里の小女人 提交于 2019-12-04 02:46:57
>>> itertools.izip('ABCD', 'xy') Traceback (most recent call last): File "<pyshell#55>", line 1, in <module> itertools.izip('ABCD', 'xy') AttributeError: 'module' object has no attribute 'izip' In Python 3, there is no izip function in the itertools module because the builtin zip function (which doesn't require any imports to access) now behaves like itertools.izip did in Python 2. So, to make your code work, just use zip instead of itertools.izip . You also mentioned an issue with string.maketrans . That's another function that is no longer in a module in Python 3. It's now a method of the

Python: faster alternative for itertools.product()?

守給你的承諾、 提交于 2019-12-04 01:44:42
问题 I'm trying to find all possible combinations of a list with length = 22 & element values = 1-9. When I use [i for i in itertools.product(range(1, 10), repeat=22)] , Python crashes. Does Python have a faster alternative? 回答1: As everyone commented, try using the generator directly instead of using a list. finding all combinations is unclear. If you need to print them, do this: for i in itertools.product(range(1, 10), repeat=22): ... #Don't actually print, that may block your computer for a

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

孤街醉人 提交于 2019-12-04 00:23:09
问题 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) ] # [()] Updates Thanks for all of the answers -- very informative. Wikipedia's discussion of the Nullary Cartesian Product provides a definitive statement: The

Python itertools.combinations: how to obtain the indices of the combined numbers

社会主义新天地 提交于 2019-12-03 23:57:17
The result created by Python's itertools.combinations() is the combinations of numbers. For example: a = [7, 5, 5, 4] b = list(itertools.combinations(a, 2)) # b = [(7, 5), (7, 5), (7, 4), (5, 5), (5, 4), (5, 4)] But I would like to also obtain the indices of the combinations such as: index = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] How can I do it? You can use enumerate: >>> a = [7, 5, 5, 4] >>> list(itertools.combinations(enumerate(a), 2)) [((0, 7), (1, 5)), ((0, 7), (2, 5)), ((0, 7), (3, 4)), ((1, 5), (2, 5)), ((1, 5), (3, 4)), ((2, 5), (3, 4))] >>> b = list((i,j) for ((i,_),(j,_))