itertools

Python cartesian product and conditions?

China☆狼群 提交于 2019-12-05 18:55:58
In Python I am using the itertools.product() function to generate input parameters for a simulation. I have a test function that requires 4 input parameters a1, a2, b1 and b2. I use the following code generate the parameters. Example: params = itertools.product(range(10,41,2), range(10,41,2), range(0, 2), range(5, 31, 5)) … which gives me 3072 combinations. Unfortunately some combinations logically make no sense. E. g. if a2 is larger than a1 the test results are useless, also when b1 equals 0 the value of b2 is completely irrelevant – so it wouldn’t make sense to test such combinations. Is

How do I iterate over a large number of tuples of integers in the order of their sum?

坚强是说给别人听的谎言 提交于 2019-12-05 18:49:50
I'm using itertools.combinations() to iterate over tuples of integers. I am interested in the tuple with the lowest sum that satisfies some conditions: def findLowestNiceTuple: for tup in itertools.combinations(range(1, 6), 2): if niceTuple(tup): return tup The generator's default order is not in the order of the elements' sum. For example: >>> itertools.combinations(range(1, 6), 2) gives a generator which will yield the following elements: [(1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)] As you can see, the sum of (1, 5) is larger than that of (2,3). For early

Python Groupby statement

北城以北 提交于 2019-12-05 18:49:11
I mam trying to group the following details list: details = [('20130325','B'), ('20130320','A'), ('20130325','B'), ('20130320','A')] >>for k,v in itertools.groupby(details,key=operator.itemgetter(0)): >> print k,list(v) And this is the output with the above groupby statement: 20130325 [('20130325', 'B')] 20130320 [('20130320', 'A')] 20130325 [('20130325', 'B')] 20130320 [('20130320', 'A')] But my expected output was: 20130325 [('20130325', 'B'),('20130325', 'B')] 20130320 [('20130320', 'A'),('20130320', 'A')] Am I doing wrong somewhere? You have to sort your details first: details.sort(key

Missing a sequence when using itertools combonations_with_replacement

五迷三道 提交于 2019-12-05 18:29:48
from itertools import combinations_with_replacement x = 'opo' v = combinations_with_replacement(x, len(x)) ans = [''.join(map(str, x)) for x in v] print(" ".join(set(ans))) I'm not sure why im missing the sequence pop here. Why does pop not show but ppo and opp do . expected output opp ppp poo ppo ooo opo oop pop actual output opp ppp poo ppo ooo opo oop Consider this: >>> x = 'abc' >>> v = itertools.combinations_with_replacement(x, len(x)) >>> ans = [''.join(map(str, x)) for x in v] >>> ans ['aaa', 'aab', 'aac', 'abb', 'abc', 'acc', 'bbb', 'bbc', 'bcc', 'ccc'] The values in the sequence are

Difference between `return iterator` and `yield from iterator`

[亡魂溺海] 提交于 2019-12-05 18:03:35
I'm trying to implement my own version of itertools.compress , the problem is that i stumbled upon the return type. I mean both of these functions return an iterator, but i think the second one is not considered a generator function because there is no yield statement inside. So my question is, are these two implementations equivalent ? def compress (seq, selectors): from operator import itemgetter fst = itemgetter (0) snd = itemgetter (1) yield from map (fst, filter (snd, zip (seq, selectors))) def compress (seq, selectors): from operator import itemgetter fst = itemgetter (0) snd =

Python product of infinite generators

帅比萌擦擦* 提交于 2019-12-05 17:27:22
I'm trying to get the product of 2 infinite generators but the product function in itertools doesn't allow this sort of behaviour. Example behaviour: from itertools import * i = count(1) j = count(1) x = product(i, j) [Killed] What I want: x = product(i, j) ((0,0), (0,1), (1,0), (1,1) ...) It doesn't matter in which order the combinations get returned as long as given infinite time, all combinations will be eventually generated. This means that given a combination of elements, there must be a finite index in the returned generator with that combination. tl;dr The code presented below is now

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

社会主义新天地 提交于 2019-12-05 12:28:57
问题 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? 回答1: You can use enumerate: >>> a = [7, 5, 5, 4] >>> list(itertools.combinations(enumerate(a), 2)) [((0, 7), (1, 5)), ((0, 7), (2, 5)), (

Extract a list from itertools.cycle

守給你的承諾、 提交于 2019-12-05 11:29:21
I have a class which contains a itertools.cycle instance which I would like to be able to copy. One approach (the only one I can come up with), is to extract the initial iterable (which was a list), and store the position that the cycle is at. Unfortunately I am unable to get hold of the list which I used to create the cycle instance, nor does there seem to be an obvious way to do it: import itertools c = itertools.cycle([1, 2, 3]) print dir(c) ['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__',

multiprocessing on tee'd generators

心已入冬 提交于 2019-12-05 09:43:33
Consider the following script in which I test two ways of performing some calculations on generators obtained by itertools.tee : #!/usr/bin/env python3 from sys import argv from itertools import tee from multiprocessing import Process def my_generator(): for i in range(5): print(i) yield i def double(x): return 2 * x def compute_double_sum(iterable): s = sum(map(double, iterable)) print(s) def square(x): return x * x def compute_square_sum(iterable): s = sum(map(square, iterable)) print(s) g1, g2 = tee(my_generator(), 2) try: processing_type = argv[1] except IndexError: processing_type = "no

Pythonic iteration over sliding window pairs in list?

你离开我真会死。 提交于 2019-12-05 03:14:50
What's the most Pythonic efficient way to iterate over a list in sliding pairs? Here's a related example: >>> l ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> for x, y in itertools.izip(l, l[1::2]): print x, y ... a b b d c f this is iteration in pairs, but how can we get iteration over a sliding pair? Meaning iteration over the pairs: a b b c c d d e etc. which is iteration over the pairs, except sliding the pair by 1 element each time rather than by 2 elements. thanks. How about: for x, y in itertools.izip(l, l[1:]): print x, y You can go even simpler. Just zip the list and the list offset by one.