itertools

Numpy with Combinatoric generators: How does one speed up Combinations?

五迷三道 提交于 2019-12-12 21:54:34
问题 It is my understanding that the itertools functions are written in C. If i wanted to speed this example code up: import numpy as np from itertools import combinations_with_replacement def combinatorics(LargeArray): newArray = np.empty((LargeArray.shape[0],LargeArray.shape[0])) for x, y in combinations_with_replacement(xrange(LargeArray.shape[0]), r=2): z = LargeArray[x] + LargeArray[y] newArray[x, y] = z return newArray Since combinations_with_replacement is written in C, does that imply that

convert itertools array into numpy array

末鹿安然 提交于 2019-12-12 17:37:14
问题 I'm creating this array: A=itertools.combinations(range(6),2) and I have to manipulate this array with numpy, like: A.reshape(.. If the dimensions is A is high, the command list(A) is too slow. How can I "convert" an itertools array into a numpy array? Update 1: I've tried the solution of hpaulj, in this specific situation is a little bit slower, any idea? start=time.clock() A=it.combinations(range(495),3) A=np.array(list(A)) print A stop=time.clock() print stop-start start=time.clock() A=np

Using itertools to group consecutive tuples by second value

*爱你&永不变心* 提交于 2019-12-12 17:36:01
问题 I have a set of data in the form: X1 = [(1,1),(3,1),(5,0),(3,0),(2,1)] I can't figure out how to group them such that: X2 = [[(1,1),(3,1)],[(5,0),(3,0)],[(2,1)]] i.e. they are grouped in a consecutive fashion by the second value in each tuple. I know it's something with this: http://docs.python.org/2/library/itertools.html#itertools.groupby 回答1: from itertools import groupby from operator import itemgetter X2 = [list(group) for key, group in groupby(X1, itemgetter(1))] Pass a key function to

How to generate lists from a specification of element combinations

纵然是瞬间 提交于 2019-12-12 13:23:07
问题 I want to generate a bunch of lists using combinations of elements specified in a form like the following: [[10, 20], [30, 40], [50, 60]] This means that the values available for the first element are 10 and 20, the values available for the second element are 30 and 40 and so on (I've used just two element options for each element for brevity; there could be more than that). I want to use this specification to generate all lists using the combinations of these elements (including the

Does python have a built-in function for interleaving generators/sequences?

安稳与你 提交于 2019-12-12 09:33:48
问题 I noticed that itertools does not (it seems to me) have a function capable of interleaving elements from several other iterable objects (as opposed to zipping them): def leaf(*args): return (it.next() for it in cycle(imap(chain,args))) tuple(leaf(['Johann', 'Sebastian', 'Bach'], repeat(' '))) => ('Johann', ' ', 'Sebastian', ' ', 'Bach', ' ') (Edit) The reason I ask is because I want to avoid unnecessary zip/flatten occurrences. Obviously, the definition of leaf is simple enough, but if there

Best way to enumerate a cartesian product with labels in python?

限于喜欢 提交于 2019-12-12 09:01:49
问题 Given a dictionary mapping variables to possible outcomes: { 'lblA' : [False, True], 'lblB' : [False, True], 'lblC' : [0,1,2] } I want to enumerate all possible dictionary outcomes: [ { 'lblA' : False , 'lblB' : False, 'lblC' : 0 }, { 'lblA' : True , 'lblB' : False, 'lblC' : 0 }, { 'lblA' : False , 'lblB' : True, 'lblC' : 0 }, { 'lblA' : True , 'lblB' : True, 'lblC' : 0 }, { 'lblA' : False , 'lblB' : False, 'lblC' : 1 }, { 'lblA' : True , 'lblB' : False, 'lblC' : 1 }, { 'lblA' : False , 'lblB

Pythonic way of copying an iterable object

青春壹個敷衍的年華 提交于 2019-12-12 08:54:16
问题 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()

Modifying a list while traversing it

核能气质少年 提交于 2019-12-12 05:48:19
问题 Is there a way to modify a list while iterating it. I should generate a list by applying some operations between items, it should be more simple to update the list l while traversing it. Is there any hiding feature in python or itertools that I can use to make this as one line code. Taking this quick example. l=[1,2,3,4,5] a=[0] for i,item in enumerate(l): a+=[item**2-a[-1]] l+=a print l It should be something like: for i,item in enumerate(l): # Update List L print l 回答1: You can iterate over

Stream all unordered/shuffled unique permutation of elements in a nested list (list of list) in Python?

五迷三道 提交于 2019-12-12 04:04:10
问题 I have two-level nested list like following. [[0, 1], [2], [3, 4], [5, 5], [6], [7], [8], [9], [10, 11], [12]] I want to generate all 8 unique permutations of this nested list, but my application absolutely needs the output to be (pseudo-)randomized and unordered. Usually, permutation strategies produce the permutations in order, but I want to be able to produce all permutations out of order. Moreover, this MUST be done through some generator, as the nested list can be very long, and number

Itertools Groupby looping over different columns

元气小坏坏 提交于 2019-12-12 04:04:09
问题 'm trying to do a conditional sum-product in Python. The simplified idea is as follows: A = [1 1 2 3 3 3] B = [0.50 0.25 0.99 0.80 0.70 0.20] I would like to have as output Total1 = 0.50*1 + 0.25*1 Total2 = 0.99*2 Total3 = 0.80*3 + 0.70*3 + 0.20*3 Thanks to the support by people over here, this part worked out! Next function I like to add, is being able to calculate this for different columns 'B' (say B1, B2, B3, ...) (with different values). These are stored in Excel and I read them out to