itertools

Does itertools.product evaluate its arguments lazily?

孤街醉人 提交于 2019-12-01 17:56:27
The following never prints anything in Python 3.6 from itertools import product, count for f in product(count(), [1,2]): print(f) Instead, it just sits there and burns CPU. The issue seems to be that product never returns an iterator if it's over an infinite space because it evaluates the full product first. This is surprising given that the product is supposed to be a generator. I would have expected this to start counting up (to infinity), something like the behavior of this generator (taken directly from the docs ): for tup in ((x,y) for x in count() for y in [1,2]): print(tup) But whereas

Itertools Combinations No Repeats: Where rgb is equivelant to rbg etc

戏子无情 提交于 2019-12-01 13:43:04
I'm trying to use itertools.combinations to return unique combinations. I've searched through several similar questions but have not been able to find an answer. An example: >>> import itertools >>> e = ['r','g','b','g'] >>> list(itertools.combinations(e,3)) [('r', 'g', 'b'), ('r', 'g', 'g'), ('r', 'b', 'g'), ('g', 'b', 'g')] For my purposes, (r,g,b) is identical to (r,b,g) and so I would want to return only (rgb),(rgg) and (gbg). This is just an illustrative example and I would want to ignore all such 'duplicates'. The list e could contain up to 5 elements. Each individual element would be

Python itertools.product with arbitrary number of sets

放肆的年华 提交于 2019-12-01 10:44:25
I wish to execute the following code: temp = [] temp.append([1,2]) temp.append([3,4]) temp.append([5,6]) print list(itertools.product(temp[0],temp[1],temp[2])) However, I would like to execute it for temp with arbitrary length. I.e. something more like: print list(itertools.product(temp)) How do I format the input correctly for itertools.product to produce the same result in the first segment of code without explicitly knowing how many entries there are in temp? print list(itertools.product(*temp)) Use * to unpack the argument iterable as separate positional arguments. Or can do that: Combine

Performance of library itertools compared to python code

坚强是说给别人听的谎言 提交于 2019-12-01 08:28:05
As answer to my question Find the 1 based position to which two lists are the same I got the hint to use the C-library itertools to speed up things. To verify I coded the following test using cProfile: from itertools import takewhile, izip def match_iter(self, other): return sum(1 for x in takewhile(lambda x: x[0] == x[1], izip(self, other))) def match_loop(self, other): element = -1 for element in range(min(len(self), len(other))): if self[element] != other[element]: element -= 1 break return element +1 def test(): a = [0, 1, 2, 3, 4] b = [0, 1, 2, 3, 4, 0] print("match_loop a=%s, b=%s,

Iterate over array twice (cartesian product) but consider only half the elements

半城伤御伤魂 提交于 2019-12-01 07:55:15
问题 I am trying to iterate over an array twice to have pairs of elements (e_i,e_j) but I only want the elements such that i < j. Basically, what I want would look like this is C-like languages. int my_array[N] = ...; for (int i=0; i<N; i++) for (int j=i+1; j<N; j++) something(my_array[i],my_array[j]); I didn't find what I was looking for in itertools (the closest thing I've found was itertools.product(*iterables[, repeat]) ). I tried a few things but I am not really convinced by any of them : my

Python itertools.product with arbitrary number of sets

筅森魡賤 提交于 2019-12-01 07:34:38
问题 I wish to execute the following code: temp = [] temp.append([1,2]) temp.append([3,4]) temp.append([5,6]) print list(itertools.product(temp[0],temp[1],temp[2])) However, I would like to execute it for temp with arbitrary length. I.e. something more like: print list(itertools.product(temp)) How do I format the input correctly for itertools.product to produce the same result in the first segment of code without explicitly knowing how many entries there are in temp? 回答1: print list(itertools

Python Itertools permutations only letters and numbers

爷,独闯天下 提交于 2019-12-01 07:34:33
I need to get only the permutations that have letters and numbers (The permutation can not be. "A, B, C, D" I need it like this: "A, B, C, 1") In short, the permutations can not contain only letters, not just numbers. Must be a combination of both. My code: import itertools print list(itertools.combinations([0,1,2,3,4,'a','b','c','d'], 4)) Then I get: [(0, 1, 2, 3), (0, 1, 2, 4), (0, 1, 2, 'a'), (0, 1, 2, 'b'), (0, 1, 2, 'c'), (0, 1, 2, 'd'), (0, 1, 3, 4), (0, 1, 3, 'a'), (0, 1, 3, 'b'), (0, 1, 3, 'c'), (0, 1, 3, 'd'), (0, 1, 4, 'a'), (0, 1, 4, 'b'), (0, 1, 4, 'c'), (0, 1, 4, 'd'), (0, 1, 'a',

groupby() giving an empty list [duplicate]

独自空忆成欢 提交于 2019-12-01 06:43:30
This question already has an answer here: difference between dict(groupby) and groupby [duplicate] 4 answers I've executed the following script: from itertools import groupby from pprint import pprint as prnt dt = [('23271800', 0.00066790780636275307), ('23271812', 0.0010018617095441298), ('26112103', 0.00066790780636275307), ('27111616', 0.0056772163540834012), # ... many lines deleted ... ('40161500', 0.00040074468381765189) ] agg = groupby(dt, lambda x: x[0]) lst = list(agg) lst1 = map(lambda x: (x[0], list(x[1])), lst) prnt(lst1) For the item '23271800' it should report [('23271800', 0

Aggregate all dataframe row pair combinations using pandas

谁都会走 提交于 2019-12-01 04:35:07
I use python pandas to perform grouping and aggregation across data frames, but I would like to now perform specific pairwise aggregation of rows (n choose 2, statistical combination). Here is the example data, where I would like to look at all pairs of genes in [mygenes]: import pandas import itertools mygenes=['ABC1', 'ABC2', 'ABC3', 'ABC4'] df = pandas.DataFrame({'Gene' : ['ABC1', 'ABC2', 'ABC3', 'ABC4','ABC5'], 'case1' : [0,1,1,0,0], 'case2' : [1,1,1,0,1], 'control1':[0,0,1,1,1], 'control2':[1,0,0,1,0] }) >>> df Gene case1 case2 control1 control2 0 ABC1 0 1 0 1 1 ABC2 1 1 0 0 2 ABC3 1 1 1

groupby() giving an empty list [duplicate]

爷,独闯天下 提交于 2019-12-01 04:24:22
问题 This question already has answers here : difference between dict(groupby) and groupby [duplicate] (4 answers) Closed 5 years ago . I've executed the following script: from itertools import groupby from pprint import pprint as prnt dt = [('23271800', 0.00066790780636275307), ('23271812', 0.0010018617095441298), ('26112103', 0.00066790780636275307), ('27111616', 0.0056772163540834012), # ... many lines deleted ... ('40161500', 0.00040074468381765189) ] agg = groupby(dt, lambda x: x[0]) lst =