itertools

Combinations with limited repeats in Python

邮差的信 提交于 2019-12-10 17:45:14
问题 I know how to get ALL combinations of a list in Python with itertools, but what if I want to limit the amount of repeats? So, if I have [1, 2, 3, 4, 5] But I want to limit combinations to only 3 repeats of each item (with a fixed length of the final list, say 10): [1, 1, 1, 2, 3, 3, 5, 5, 5, 4] [1, 2, 3, 3, 3, 4, 5, 5, 4, 4] [4, 4, 1, 1, 1, 5, 2, 2, 2, 3] and so on. How do I do this? 回答1: This would work: import random L = [1, 2, 3, 4, 5] L3 = L * 3 random.shuffle(L3) L3[:10] 回答2: I don't

Group and combine items of multiple-column lists with itertools/more-itertools in Python

微笑、不失礼 提交于 2019-12-10 17:15:59
问题 This code: from itertools import groupby, count L = [38, 98, 110, 111, 112, 120, 121, 898] groups = groupby(L, key=lambda item, c=count():item-next(c)) tmp = [list(g) for k, g in groups] Takes [38, 98, 110, 111, 112, 120, 121, 898] , groups it by consecutive numbers and merge them with this final output: ['38', '98', '110,112', '120,121', '898'] How can the same be done with a list of lists with multiple columns, like this list below where you can group them by name and the consecution of its

How to build “vectorized” building blocks using itertools module?

旧巷老猫 提交于 2019-12-10 16:37:47
问题 The recipe section of itertools docs begins with this text: The extended tools offer the same high performance as the underlying toolset. The superior memory performance is kept by processing elements one at a time rather than bringing the whole iterable into memory all at once. Code volume is kept small by linking the tools together in a functional style which helps eliminate temporary variables. High speed is retained by preferring “vectorized” building blocks over the use of for-loops and

paralellize loop over iter

匆匆过客 提交于 2019-12-10 16:31:26
问题 I am having performance issues with my code. step # IIII consumes hours of time. I used to materialize the the itertools.prodct before, but thanks to a user I dont do pro_data = product(array_b,array_a) anymore. This helped me with memory issues, but the still is heavily time consuming. I would like to paralellize it with multithreading or multiprocesisng, whatever you can suggest, I am grateful. Explanation. I have two arrays that contain x and y values of particles. For each particle

python: is there a library function for chunking an input stream?

天涯浪子 提交于 2019-12-10 13:27:03
问题 I want to chunk an input stream for batch processing. Given an input list or generator, x_in = [1, 2, 3, 4, 5, 6 ...] I want a function that will return chunks of that input. Say, if chunk_size=4 , then, x_chunked = [[1, 2, 3, 4], [5, 6, ...], ...] This is something I do over and over, and was wondering if there is a more standard way than writing it myself. Am I missing something in itertools ? (One could solve the problem with enumerate and groupby , but that feels clunky.) In case anyone

Python itertools.product with variable number of arguements

空扰寡人 提交于 2019-12-10 13:05:16
问题 I am trying to write a module to combine a variable number of lists using itertools.product. The closest I can get is: import itertools lists = [["item1","item2"],["A","b","C"], ["etc..."]] searchterms = list(itertools.product(lists)) print searchterms This doesn't work, because lists is a single list, so it just returns the original sequence. But I can't figure out how to pass each element of the lists variable to itertools. Thanks for any suggestions. 回答1: You need to use * to separate the

Python 3, module 'itertools' has no attribute 'ifilter'

时间秒杀一切 提交于 2019-12-10 12:43:24
问题 I am new at Python, trying to build an old python file into Python 3. I got several build errors which I solved. But at this point I am getting above error. I have no idea how to fix this. The code section looks like below. return itertools.ifilter(lambda i: i.state == "IS", self.storage) 回答1: itertools.ifilter() was removed in Python 3 because the built-in filter() function provides the same functionality now. If you need to write code that can run in both Python 2 and Python 3, use imports

cycle through multiple list using itertools.cycle()

有些话、适合烂在心里 提交于 2019-12-10 12:32:47
问题 I have a list of servers. Every server has a list of name on it. example: server1 = ['a','b','c'] server2 = ['d','e','f'] server3 = ['g','h','i'] I want to iterate per server name not per server. For example after picking 'a' in server1 , move to 'd' (not 'b' ) and so on. If I'm going to use itertools.cycle() , do I have to create a list of server to cycle through? My expected result is ['a','d','g','b','e','h','c','f','i'] . Can you give me a simple example on how to cycle in multiple list.

How to flatten a list of lists of lists in python [duplicate]

℡╲_俬逩灬. 提交于 2019-12-10 12:00:30
问题 This question already has answers here : Flatten an irregular list of lists (44 answers) Closed 4 years ago . I've seen a couple answers on how to flatten lists of the form [1,[1,2],[3]] print list(itertools.chain(*[1,[1,2],[3]])) but how do you flatten lists like this: [[1],[[1,2],[3]]] print list(itertools.chain(*[[1],[[1,2],[3]]])) [1, [1, 2], [3]] 回答1: I usually use this recipe: import collections def flatten(l): for el in l: if isinstance(el, collections.Iterable) and not isinstance(el,

Python itertools.combinations() memory problems

对着背影说爱祢 提交于 2019-12-10 10:35:08
问题 I'm processing a huge number of combinations of items (from League of Legends), about 72 million, all of which are fed into a function that calculates how beneficial they are. We're trying to find the best possible combination. Ignoring the fact that there might be better ways, algorithmically speaking, to do this, can anyone tell me why I'm getting a memory error? allpossiblei = itertools.combinations(items.keys(),5) maxc = 0 i = 0 for combo in allpossiblei: icombo = [items[name] for name in