itertools

How to find indices and combinations that adds upto given sum?

99封情书 提交于 2020-07-07 10:32:49
问题 How to find the combinations and corresponding indices that adds upto given sum ? And also, can it be handled list of elements of size 500000 (higher size) ? Input: l1 = [9,1, 2, 7, 6, 1, 5] target = 8 **Constraints** 1<=(len(l1))<=500000 1<=each_list_element<=1000 Output: Format : {index:element} {1:1, 5:1, 4:6} #Indices : 1,5,4 Elements : 1,1,6 {1:1, 2:2, 6:5} {5:1, 2:2, 6:5} {1:1, 3:7} {5:1, 3:7} {2:2, 4:6} Tried: from itertools import combinations def test(l1, target): l2 = [] l3 = [] if

How to get Cartesian product of two iterables when one of them is infinite

戏子无情 提交于 2020-07-06 05:24:08
问题 Let's say I have two iterables, one finite and one infinite: import itertools teams = ['A', 'B', 'C'] steps = itertools.count(0, 100) I was wondering if I can avoid the nested for loop and use one of the infinite iterators from the itertools module like cycle or repeat to get the Cartesian product of these iterables. The loop should be infinite because the stop value for steps is unknown upfront. Expected output: $ python3 test.py A 0 B 0 C 0 A 100 B 100 C 100 A 200 B 200 C 200 etc... Working

How to get Cartesian product of two iterables when one of them is infinite

最后都变了- 提交于 2020-07-06 05:23:51
问题 Let's say I have two iterables, one finite and one infinite: import itertools teams = ['A', 'B', 'C'] steps = itertools.count(0, 100) I was wondering if I can avoid the nested for loop and use one of the infinite iterators from the itertools module like cycle or repeat to get the Cartesian product of these iterables. The loop should be infinite because the stop value for steps is unknown upfront. Expected output: $ python3 test.py A 0 B 0 C 0 A 100 B 100 C 100 A 200 B 200 C 200 etc... Working

How to calculate a Cartesian product of a list with itself [duplicate]

狂风中的少年 提交于 2020-07-03 05:42:18
问题 This question already has answers here : Get the cartesian product of a series of lists? (13 answers) Closed 3 years ago . For example, list = [0, 1, 2] I want a list of all possible 2-combinations: combinations = [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2)] It seems to me that all the tools in itertools in Python only make one of (1,0) and (0,1), not both, I need both. Any suggestions, other than entering them by hand? 回答1: You are looking for a Cartesian product of that

How to make combination, if any one of the element exists that can be added to make sum?

半世苍凉 提交于 2020-06-29 03:49:55
问题 To find all possible combinations that can be added to make given sum. Combinations can be formed with multiple elements and also if any single element exists. Input: l1 = [9,1, 2, 7, 6, 1, 5] target = 8 **Constraints** 1<=(len(l1))<=500000 1<=each_list_element<=1000 Output: Format : {index:element} {1:1, 5:1, 4:6} #Indices : 1,5,4 Elements : 1,1,6 {1:1, 2:2, 6:5} {5:1, 2:2, 6:5} {1:1, 3:7} {5:1, 3:7} {2:2, 4:6} More Scenarios: Input = [4,6,8,5,3] target = 3 Output {4:3} Input = [4,6,8,3,5,3]

Python: breaking a list into all possible sublists

爷,独闯天下 提交于 2020-06-26 05:59:25
问题 Lets assume I've got a list of integers: mylist = [101, 102, 103, 104, 105, 106] Now I need to create every possible sublist division (order preserved): sublists = [([101], [102, 103, 104, 105, 106]), ([101, 102], [103, 104, 105, 106]), ([101, 102, 103], [104, 105, 106]), ... ([101, 102], [103, 104], [105, 106]), ... ([101], [102, 103, 104], [105], [106]), ... ([101], [102], [103], [104], [105], [106])] Any idea? Would itertools be helpful? 回答1: You are creating slice points ; are you slicing

python itertools product repeat to big

烂漫一生 提交于 2020-06-23 13:26:47
问题 I'm trying to make a python script to calculate some win/loss chances. to do this i'm trying to get all possible combinations off wins and losses (K is the number of wins needed to win the game): for combination in itertools.product(['W','L'], repeat=(K*2)-1): if ((combination.count('L') < K) and (combination.count('W') == K)): #calculate the chance of this situation happening for some reason this works fine, until the repeat becomes to big (for instance if K=25) Can someone give me some

python itertools product repeat to big

本秂侑毒 提交于 2020-06-23 13:26:10
问题 I'm trying to make a python script to calculate some win/loss chances. to do this i'm trying to get all possible combinations off wins and losses (K is the number of wins needed to win the game): for combination in itertools.product(['W','L'], repeat=(K*2)-1): if ((combination.count('L') < K) and (combination.count('W') == K)): #calculate the chance of this situation happening for some reason this works fine, until the repeat becomes to big (for instance if K=25) Can someone give me some

itertools 'previous' (opposite of next) python

情到浓时终转凉″ 提交于 2020-05-14 19:07:08
问题 I'm currently using something like >> import itertools >> ABC = [a, b, c] >> abc = itertools.cycle( ABC ) >> next( abc ) a >> next( abc ) b >> next( abc ) c I want my next call to be >> previous( abc ) b Is there a method in itertools that can accomplish this? 回答1: No, there isn't. Because of the way Python's iteration protocol works, it would be impossible to implement previous without keeping the entire history of the generated values. Python doesn't do this, and given the memory

itertools 'previous' (opposite of next) python

天涯浪子 提交于 2020-05-14 19:05:09
问题 I'm currently using something like >> import itertools >> ABC = [a, b, c] >> abc = itertools.cycle( ABC ) >> next( abc ) a >> next( abc ) b >> next( abc ) c I want my next call to be >> previous( abc ) b Is there a method in itertools that can accomplish this? 回答1: No, there isn't. Because of the way Python's iteration protocol works, it would be impossible to implement previous without keeping the entire history of the generated values. Python doesn't do this, and given the memory