itertools

Merged iterators produce obscure results

不想你离开。 提交于 2019-12-11 03:47:44
问题 I'm trying to implement prime number generator using Sieve of Eratosthenes algorithm. I do it just to try using recursive iterator merging to implement sifter. What I do is this: from itertools import count,islice,groupby from heapq import merge def primes3(): p = 2 yield p sifter = (i*p for i in count(p)) s = next(sifter) for p in count(p+1): if p==s: # this p is sieved out print('s: {}'.format(s)) s = next(sifter) else: yield p # this is prime print('p: {}'.format(p)) sifter = (k for k, g

How to check subsequence exists in a list? [duplicate]

空扰寡人 提交于 2019-12-11 03:22:11
问题 This question already has answers here : Check for presence of a sliced list in Python (11 answers) Closed 4 years ago . In python, it's possible to use the is keyword to check for contains, e.g. >>> 3 in [1,2,3,4,5] True But this doesn't yield the same output if it's checking whether a list of a single integer is inside the reference list [1,2,3,4,5] : >>> [3] in [1,2,3,4,5] False Also, checking a subsequence in the reference list cannot be achieved with: >>> [3,4,5] in [1,2,3,4,5] False Is

Generate kth combination without generating/iterating previous

时光总嘲笑我的痴心妄想 提交于 2019-12-11 02:55:24
问题 Given a set of items, for example: [ 1, 2, 3, 4, 5, 6 ] I'd like to generate all possible combinations of a certain length with repetition. The twist is I'd like to start at a predetermined combination (a sort of offset into the list of combinations). For example, starting with this: [ 1, 5, 6 ] The first (next) combination would be: [ 1, 6, 6 ] I've had success using itertools.combinations_with_replacement() to generate the combinations, but the project this is for will require working with

How to create an iterator that produces items where no item has a single character represented more than n number times in python?

五迷三道 提交于 2019-12-11 02:36:44
问题 I have created a script that uses the following code to iterate over all combinations of characters in the sCharacters string: sCharacters = "abcdefghijklmnopqrstuvwxyz0123456789" iKeyLength = len(sCharacters) for sCharacterCombination in itertools.product(sCharacters, repeat=iKeyLength): # Do Stuff Here However I am only interested in combinations where no single character is represented more than n number of times in the sCharacterCombination. Eg; I want to filter out strings like

Group list of tuples by item

橙三吉。 提交于 2019-12-11 00:34:04
问题 I have this list as example: [(148, Decimal('3.0')), (325, Decimal('3.0')), (148, Decimal('2.0')), (183, Decimal('1.0')), (308, Decimal('1.0')), (530, Decimal('1.0')), (594, Decimal('1.0')), (686, Decimal('1.0')), (756, Decimal('1.0')), (806, Decimal('1.0'))] Now i want to group by the id, so I will use itemgetter(0) : import operator, itertools from decimal import * test=[(148, Decimal('3.0')), (325, Decimal('3.0')), (148, Decimal('2.0')), (183, Decimal('1.0')), (308, Decimal('1.0')), (530,

Faster Way To Simultaneously Iterate Over Rolling Window Of Two Or More Numpy Arrays?

怎甘沉沦 提交于 2019-12-10 23:57:31
问题 I have two numpy arrays x and y. e.g. x Out[1]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) y Out[1]: array([100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]) I want to simultaneously iterate over a n period rolling window over x and y. I would like to do this as quickly as possible, while keeping the rolling windows as numpy arrays. I shamelessly stole some code from the itertools documentation. And then

Applying arithmetic operations on list of numbers without repetition in python

核能气质少年 提交于 2019-12-10 22:18:01
问题 We've got the following python list: [1,2,3,10] I would like to accomplish the following: Create a function that takes in the list and figures out from the list of arithmetic operations: ['+', '-', '/','*'] which combinations give us 6 as the answer. We don't want repetition so we don't want 2*3 and 3*2 in our solution. We do want to list the numbers we haven't used so that's (1 and 10 here). Same for 2/1*3=6.0 , 2*3/1=6.0 , 3/1*2=6.0 , 3*2/1=6.0 are all considered equivalent since we use the

List all combinations of combinations [duplicate]

微笑、不失礼 提交于 2019-12-10 20:54:51
问题 This question already has answers here : How to split a list into pairs in all possible ways (13 answers) Closed last year . I am trying to list out all the possible combinations of groups of 3 that one can make of 6 people. (A, B, C, D, E, F) The order doesn't of the group doesn't matter The order of the pair doesn't matter Possible combinations: {(B,D),(C,E),(G,H)} {(B,C),(D,E),(G,H)} {(B,E),(C,D),(G,H)} I could only get as far to write: from itertools import combinations x = combinations(

Python itertools product, but conditional?

走远了吗. 提交于 2019-12-10 19:15:27
问题 I have a function fun that takes several parameters p0,p1,.. For each parameter i give a list of possible values: p0_list = ['a','b','c'] p1_list = [5,100] I can now call my function for every combination of p0,p1 for i in itertools.product(*[p0,p1]): print fun(i) Now comes the problem: What if i already know, that the parameter p1 only has an effect on the result of fun, if p0 is 'a' or 'c'? In this case i need my list of parameter combinations to look like: [('a', 5), ('a',100), ('b', 5), (

Concatenate multiple lists of lists in python

独自空忆成欢 提交于 2019-12-10 19:13:06
问题 I have a list of multiple list-of-lists in python: a = [[[0,1,2], [10,11,12]], [[3,4,5], [13,14,15]]] And I would like to merge all the first lists together, the second lists together, and so forth: final = [[0,1,2,3,4,5], [10,11,12,13,14,15]] The furthest I've got is to try to unzip the outer lists: zip(*a) = [([0,1,2], [3,4,5]), ([10,11,12], [13,14,15])] I suppose one could loop through these and then chain each together, but that seems wasteful. What's a pythonic way to fix this? NOTE :