itertools

itertools product speed up

烈酒焚心 提交于 2019-11-26 17:46:00
I use itertools.product to generate all possible variations of 4 elements of length 13. The 4 and 13 can be arbitrary, but as it is, I get 4^13 results, which is a lot. I need the result as a Numpy array and currently do the following: c = it.product([1,-1,np.complex(0,1), np.complex(0,-1)], repeat=length) sendbuf = np.array(list(c)) With some simple profiling code shoved in between, it looks like the first line is pretty much instantaneous, whereas the conversion to a list and then Numpy array takes about 3 hours. Is there a way to make this quicker? It's probably something really obvious

Replace list of list with “condensed” list of list while maintaining order

自作多情 提交于 2019-11-26 17:32:48
问题 I have a list of list as in the code I attached. I want to link each sub list if there are any common values. I then want to replace the list of list with a condensed list of list. Examples: if I have a list [[1,2,3],[3,4]] I want [1,2,3,4] . If I have [[4,3],[1,2,3]] I want [4,3,1,2] . If I have [[1,2,3],[a,b],[3,4],[b,c]] I want [[1,2,3,4],[a,b,c]] or [[a,b,c],[1,2,3,4]] I don't care which one. I am almost there... My problem is when I have a case like [[1,2,3],[10,5],[3,8,5]] I want [1,2,3

How to turn an itertools “grouper” object into a list

允我心安 提交于 2019-11-26 17:04:10
问题 I am trying to learn how to use itertools.groupby in Python and I wanted to find the size of each group of characters. At first I tried to see if I could find the length of a single group: from itertools import groupby len(list(list( groupby("cccccaaaaatttttsssssss") )[0][1])) and I would get 0 every time. I did a little research and found out that other people were doing it this way: from itertools import groupby for key,grouper in groupby("cccccaaaaatttttsssssss"): print key,len(list

Prevent memory error in itertools.permutation

限于喜欢 提交于 2019-11-26 16:41:18
问题 Firstly I would like to mention that i have a 3 gb ram. I am working on an algorithm that is exponential in time on the nodes so for it I have in the code perm = list( itertools.permutations(list(graph.Nodes))) # graph.Nodes is a tuple of 1 , 2 , ... n integers which generates all the combinations of vertices in a list and then i can work on one of the permutation. However when i run the program for 40 vertices , it gives a memory error. Is there any simpler way in implementation via which i

N-D version of itertools.combinations in numpy

久未见 提交于 2019-11-26 16:26:33
问题 I would like to implement itertools.combinations for numpy. Based on this discussion, I have a function that works for 1D input: def combs(a, r): """ Return successive r-length combinations of elements in the array a. Should produce the same output as array(list(combinations(a, r))), but faster. """ a = asarray(a) dt = dtype([('', a.dtype)]*r) b = fromiter(combinations(a, r), dt) return b.view(a.dtype).reshape(-1, r) and the output makes sense: In [1]: list(combinations([1,2,3], 2)) Out[1]: [

How to apply itertools.product to elements of a list of lists?

青春壹個敷衍的年華 提交于 2019-11-26 13:05:14
问题 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 = [((-1, 1),), ((-2, 2),), ((-3, 3),)] But what I want to get is cp1 = [(-1,-2,-3), (-1,-2,+3), (-1,+2,-3), (-1,+2,+3), ..., (+1,+2,-3), (+1,+2,+3)]. I have tried a few

When is it better to use zip instead of izip?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 12:22:21
问题 When is it better to use zip instead of itertools.izip? 回答1: When you know you'll want the full list of items constructed (for instance, for passing to a function that would modify that list in-place). Or when you want to force the arguments you're passing to zip() to be completely evaluated at that specific point. 回答2: zip computes all the list at once, izip computes the elements only when requested. One important difference is that 'zip' returns an actual list, 'izip' returns an 'izip

What is the purpose in Python's itertools.repeat?

ε祈祈猫儿з 提交于 2019-11-26 12:12:20
问题 Every use I can think of for Python\'s itertools.repeat() class, I can think of another equally (possibly more) acceptable solution to achieve the same effect. For example: >>> [i for i in itertools.repeat(\'example\', 5)] [\'example\', \'example\', \'example\', \'example\', \'example\'] >>> [\'example\'] * 5 [\'example\', \'example\', \'example\', \'example\', \'example\'] >>> list(map(str.upper, itertools.repeat(\'example\', 5))) [\'EXAMPLE\', \'EXAMPLE\', \'EXAMPLE\', \'EXAMPLE\', \

Python how to read N number of lines at a time

余生颓废 提交于 2019-11-26 08:08:26
问题 I am writing a code to take an enormous textfile (several GB) N lines at a time, process that batch, and move onto the next N lines until I have completed the entire file. (I don\'t care if the last batch isn\'t the perfect size). I have been reading about using itertools islice for this operation. I think I am halfway there: from itertools import islice N = 16 infile = open(\"my_very_large_text_file\", \"r\") lines_gen = islice(infile, N) for lines in lines_gen: ...process my lines... The

importing izip from itertools module gives NameError in Python 3.x

谁都会走 提交于 2019-11-26 07:44:01
问题 I am trying to import the izip module like so: from itertools import izip However after recently changing over from Python 2.7 to 3 - it doesn\'t seem to work. I am trying to write to a csv file: writer.writerows(izip(variable1,2)) But I have no luck. Still encounter an error. 回答1: In Python 3 the built-in zip does the same job as itertools.izip in 2.X(returns an iterator instead of a list). The zip implementation is almost completely copy-pasted from the old izip, just with a few names