itertools

How to group similar items in a list?

不羁的心 提交于 2019-12-22 02:03:12
问题 I am looking to group similar items in a list based on the first three characters in the string. For example: test = ['abc_1_2', 'abc_2_2', 'hij_1_1', 'xyz_1_2', 'xyz_2_2'] How can I group the above list items into groups based on the first grouping of letters (e.g. 'abc' )? The following is the intended output: output = {1: ('abc_1_2', 'abc_2_2'), 2: ('hij_1_1',), 3: ('xyz_1_2', 'xyz_2_2')} or output = [['abc_1_2', 'abc_2_2'], ['hij_1_1'], ['xyz_1_2', 'xyz_2_2']] I have tried using itertools

How to group similar items in a list?

故事扮演 提交于 2019-12-22 02:03:07
问题 I am looking to group similar items in a list based on the first three characters in the string. For example: test = ['abc_1_2', 'abc_2_2', 'hij_1_1', 'xyz_1_2', 'xyz_2_2'] How can I group the above list items into groups based on the first grouping of letters (e.g. 'abc' )? The following is the intended output: output = {1: ('abc_1_2', 'abc_2_2'), 2: ('hij_1_1',), 3: ('xyz_1_2', 'xyz_2_2')} or output = [['abc_1_2', 'abc_2_2'], ['hij_1_1'], ['xyz_1_2', 'xyz_2_2']] I have tried using itertools

python no output when using pool.map_async

让人想犯罪 __ 提交于 2019-12-22 00:29:56
问题 I am experiencing very strange issues while working with the data inside my function that gets called by pool.map. For example, the following code works as expected... import csv import multiprocessing import itertools from collections import deque cur_best = 0 d_sol = deque(maxlen=9) d_names = deque(maxlen=9) **import CSV Data1** def calculate(vals): #global cur_best sol = sum(int(x[2]) for x in vals) names = [x[0] for x in vals] print(", ".join(names) + " = " + str(sol)) def process(): pool

Name parts of iterables in itertools.products

时光怂恿深爱的人放手 提交于 2019-12-21 13:40:07
问题 I've been reading about itertools , which seems to be a very powerful module. I am particularly interested in itertools.product() which appears to give me all of the combinations of the iterable inputs. However, I would like to know which of the input iterables each of the outputs are coming from. For example, a simple standard example is: itertools.product([1, 2, 3], [1, 2]) If the user provided the inputs of [1,2,3], [1, 2] I won't know which order they came in, so getting a result of (1, 2

How to unzip an iterator?

倾然丶 夕夏残阳落幕 提交于 2019-12-21 08:03:40
问题 Given a list of pairs xys , the Python idiom to unzip it into two lists is: xs, ys = zip(*xys) If xys is an iterator, how can I unzip it into two iterators, without storing everything in memory? 回答1: Suppose you have some iterable of pairs: a = zip(range(10), range(10)) If I'm correctly interpreting what you are asking for, you could generate independent iterators for the firsts and seconds using itertools.tee: xs, ys = itertools.tee(a) xs, ys = (x[0] for x in xs), (y[1] for y in ys) Note

How to unzip an iterator?

拟墨画扇 提交于 2019-12-21 08:01:07
问题 Given a list of pairs xys , the Python idiom to unzip it into two lists is: xs, ys = zip(*xys) If xys is an iterator, how can I unzip it into two iterators, without storing everything in memory? 回答1: Suppose you have some iterable of pairs: a = zip(range(10), range(10)) If I'm correctly interpreting what you are asking for, you could generate independent iterators for the firsts and seconds using itertools.tee: xs, ys = itertools.tee(a) xs, ys = (x[0] for x in xs), (y[1] for y in ys) Note

izip_longest in itertools: what's going on here?

末鹿安然 提交于 2019-12-21 04:31:59
问题 I'm struggeling to understand how the below code works. It's from http://docs.python.org/library/itertools.html#itertools.izip_longest, and is the pure-python equivalent of the izip_longest iterator. I'm especially mystified by the sentinel function, how does it work? def izip_longest(*args, **kwds): # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- fillvalue = kwds.get('fillvalue') def sentinel(counter = ([fillvalue]*(len(args)-1)).pop): yield counter() # yields the fillvalue, or

izip_longest in itertools: what's going on here?

纵饮孤独 提交于 2019-12-21 04:31:28
问题 I'm struggeling to understand how the below code works. It's from http://docs.python.org/library/itertools.html#itertools.izip_longest, and is the pure-python equivalent of the izip_longest iterator. I'm especially mystified by the sentinel function, how does it work? def izip_longest(*args, **kwds): # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- fillvalue = kwds.get('fillvalue') def sentinel(counter = ([fillvalue]*(len(args)-1)).pop): yield counter() # yields the fillvalue, or

Why is itertools.chain faster than a flattening list comprehension?

有些话、适合烂在心里 提交于 2019-12-21 03:56:06
问题 In the context of a discussion in the comments of this question it was mentioned that while concatenating a sequence of strings simply takes ''.join([str1, str2, ...]) , concatenating a sequence of lists would be something like list(itertools.chain(lst1, lst2, ...)) , although you can also use a list comprehension like [x for y in [lst1, lst2, ...] for x in y] . What surprised me is that the first method is consistently faster than the second: import random import itertools random.seed(100)

itertools.accumulate() versus functools.reduce()

 ̄綄美尐妖づ 提交于 2019-12-20 12:28:26
问题 In Python 3.3, itertools.accumulate(), which normally repeatedly applies an addition operation to the supplied iterable, can now take a function argument as a parameter; this means it now overlaps with functools.reduce(). With a cursory look, the main differences between the two now would seem to be: accumulate() defaults to summing but doesn't let you supply an extra initial condition explicitly while reduce() doesn't default to any method but does let you supply an initial condition for use