itertools

summing all possible combinations of an arbitrary number of arrays and applying limits

若如初见. 提交于 2019-12-22 16:29:10
问题 I am trying to generate an array of all combinations of an arbitrary number of arrays. From the generated array, I would like to add an constraint that the sum of the numbers must lie between two bounds (say 'lower' and 'upper') One method of doing this is to use cartersian, sum the elements, and select the ones that fall within the lower and upper bounds. However, the main limitation is that it is possible to run out of memory given a large number of input arrays. Another method is to use

Combination of 1 and 0 in an array in Python

一世执手 提交于 2019-12-22 12:42:14
问题 I want to make a combination of 1's and 0's in a 2d array like the following: [[ 1, 1, 1, 1, 0, 0, 0, 0 ], [ 1, 1, 1, 0, 1, 0, 0, 0 ], [ 1, 1, 1, 0, 0, 1, 0, 0 ], [ 1, 1, 1, 0, 0, 0, 1, 0 ], [ 1, 1, 1, 0, 0, 0, 0, 1 ], . . . ] That means a combination of four 1's and four 0's. I have looked at the itertools module's permutations() and combinations() , but couldn't find any suitable functions to perform this combination. 回答1: You can also use combinations to generate unique combinations

Python Groupby statement

若如初见. 提交于 2019-12-22 10:51:18
问题 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'),(

How to efficiently sample combinations of rows in a pandas DataFrame

偶尔善良 提交于 2019-12-22 10:39:55
问题 Let's say I have a pandas DataFrame with a certain number of columns and rows. What I want to do is to find the combination of 5 rows that combined yield the highest score in a particular column given some threshold. Below is a little toy example to illustrate it better: Below is a simplified example of my code, and I am wondering if this "brute force" approach is a smart way to tackle this problem. Is there any chance to do it more efficiently? Using other Python libraries, or are there

Python cartesian product and conditions?

£可爱£侵袭症+ 提交于 2019-12-22 09:53:24
问题 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

Explain combination function of python module itertools

冷暖自知 提交于 2019-12-22 09:28:39
问题 I have often used itertools module in Python but it feels like cheating if I don't know the logic behind it. Here is the code to find combinations of string when order is not important. def combinations(iterable, r): # combinations('ABCD', 2) --> AB AC AD BC BD CD # combinations(range(4), 3) --> 012 013 023 123 pool = tuple(iterable) n = len(pool) if r > n: return indices = list(range(r)) yield tuple(pool[i] for i in indices) while True: for i in reversed(range(r)): if indices[i] != i + n - r

zip()-like built-in function filling unequal lengths from left with None value

我与影子孤独终老i 提交于 2019-12-22 08:52:56
问题 Is there a built-in function that works like zip(), but fills the results so that the length of the resulting list is the length of the longest input and fills the list from the left with e.g. None ? There is already an answer using zip_longest from itertools module and the corresponding question is very similar to this. But with zip_longest it seems that you can only fill missing data from the right. Here might be a use case for that, assuming we have names stored only like this (it's just

String coverage optimization in Python

谁说胖子不能爱 提交于 2019-12-22 06:46:51
问题 I have this initial string. 'bananaappleorangestrawberryapplepear' And also have a tuple with strings: ('apple', 'plepe', 'leoran', 'lemon') I want a function so that from the initial string and the tuple with strings I obtain this: 'bananaxxxxxxxxxgestrawberryxxxxxxxar' I know how to do it imperatively by finding the word in the initial string for every word and then loop character by character in all initial string with replaced words. But it's not very efficient and ugly. I suspect there

Python creating a list with itertools.product?

試著忘記壹切 提交于 2019-12-22 04:51:13
问题 I'm creating a list with itertools from a list of ranges, so far I have this: start_list = [xrange(0,201,1),xrange(0,201,2),xrange(0,201,5),xrange(0,201,10),xrange(0,201,20),xrange(0,201,50),xrange(0,201,100),xrange(0,201,200)] Now, I know that if I were to try to run this next line it will kill my python interpreter: next_list = list(itertools.product(*start_list)) What I'm wondering is would it be possible to put in an argument that checks each tuple, for a sum of its items and only puts

all permutations of +-r, +-s

大憨熊 提交于 2019-12-22 04:21:50
问题 Given two numbers r and s , I would like to get a list of all permutations of n +-r and m +-s . For example (with r=3.14 and s=2.71 ), n = 1 m = 1 out = [ (+r, +s), (+r, -s), (-r, +s), (-r, -s), (+s, +r), (+s, -r), (-s, +r), (-s, -r) ] n = 1 m = 2 out = [ (+r, +s, +s), (+r, -s, +s), (-r, +s, +s), (-r, -s, +s), ... (+s, +r, +s), (-s, +r, +s), (+s, -r, +s), (-s, -r, +s), ... ... ] With itertools.product([+r, -r], repeat=n) I can get the list of the r s and s s separately, and I'd only need to