itertools

python return lists of continuous integers from list

与世无争的帅哥 提交于 2019-11-28 11:35:52
I have a list of integers, and I want to generate a list containing a list of all the continuous integers. #I have: full_list = [0,1,2,3,10,11,12,59] #I want: continuous_integers = [[0,1,2,3], [10,11,12], [59]] I have the following which works, but seems like a poor way to do it: sub_list = [] continuous_list = [] for x in full_list: if sub_list == []: sub_list.append(x) elif x-1 in sub_list: sub_list.append(x) else: continuous_list.append(sub_list) sub_list = [x] continuous_list.append(sub_list) I've seen other questions suggesting that itertools.groupby is an efficient way to do this, but I

Generating all unique pair permutations

孤人 提交于 2019-11-28 11:19:33
I need to generate all possible pairings, but with the constraint that a particular pairing only occurs once in the results. So for example: import itertools for perm in itertools.permutations(range(9)): print zip(perm[::2], perm[1::2]) generates all possible two-paired permutations; here's a small subset of the output: ... [(8, 4), (7, 6), (5, 3), (0, 2)] [(8, 4), (7, 6), (5, 3), (1, 0)] [(8, 4), (7, 6), (5, 3), (1, 2)] [(8, 4), (7, 6), (5, 3), (2, 0)] [(8, 4), (7, 6), (5, 3), (2, 1)] [(8, 5), (0, 1), (2, 3), (4, 6)] [(8, 5), (0, 1), (2, 3), (4, 7)] [(8, 5), (0, 1), (2, 3), (6, 4)] [(8, 5),

Which itertools generator doesn't skip any combinations?

不想你离开。 提交于 2019-11-28 10:44:07
问题 When I run this code I don't get all the possible combinations of 3 characters: def comb(iterable, r): pool = tuple(iterable) n = len(pool) for indices in permutations(range(n), r): if sorted(indices) == list(indices): yield tuple(pool[i] for i in indices) def start(): for x in comb("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12234567890!@#$%^&*?,()-=+[]/;",3): print x Instead it skips some. When I repeated the characters 3 times, I got all the combinations I needed, but I get some

Itertools equivalent of nested loop “for x in xs: for y in ys…”

亡梦爱人 提交于 2019-11-28 08:19:06
问题 I have a nested loop to create all combinations in a set of conjugated verbs. The aim to to get all possible combinations of verb, person and tense, e.g. [['to be', 'first person singular', 'future'],['to be', 'second person singular', 'future'], ...] . for v in verbs: for p in persons: for t in tenses: return [v, p, t] Is there a way of reducing the nesting, perhaps using itertools ? 回答1: for v, p, t in itertools.product(verbs, persons, tenses): ... 回答2: You can use itertools.product for

itertools.cycle().next()?

百般思念 提交于 2019-11-28 08:02:00
Well, I was using itertools.cycle().next() method with Python 2.6.6, but now that I updated to 3.2 I noticed that itertools.cycle() object has no method next() . I used it to cycle a string in the spin() method of a Spinner class. So if we cycle the tuple ('|', '/', '-', '\\', '|', '/', '-') , it'll print: | , / , - , \ , | , / , - , | , / and so on... I've searched the release notes of Python 3.0, 3.1 and 3.2 and didn't noticed any change on this. When this have changed? Is there any simple alternative to achieve the same functionality as before? Thank you in advance. d0ugal iter.next() was

Group by and aggregate the values of a list of dictionaries in Python

試著忘記壹切 提交于 2019-11-28 05:57:36
I'm trying to write a function, in an elegant way, that will group a list of dictionaries and aggregate (sum) the values of like-keys. Example: my_dataset = [ { 'date': datetime.date(2013, 1, 1), 'id': 99, 'value1': 10, 'value2': 10 }, { 'date': datetime.date(2013, 1, 1), 'id': 98, 'value1': 10, 'value2': 10 }, { 'date': datetime.date(2013, 1, 2), 'id' 99, 'value1': 10, 'value2': 10 } ] group_and_sum_dataset(my_dataset, 'date', ['value1', 'value2']) """ Should return: [ { 'date': datetime.date(2013, 1, 1), 'value1': 20, 'value2': 20 }, { 'date': datetime.date(2013, 1, 2), 'value1': 10, 'value2

Python itertools permutations how to include repeating characters [duplicate]

偶尔善良 提交于 2019-11-28 02:55:24
问题 This question already has an answer here : Closed 6 years ago . Possible Duplicate: Power set and Cartesian Product of a set python With Python Itertools.permutations() I would like to receive and output of permutations with repeating characters. For an example this my function below and its current output. def perm(n,i): b = 0 while b < n: n= n -1 from itertools import permutations as p file.write('\n'.join([''.join(item) for item in p(i,n)])) perm(4,'0123') the output is: 012 013 021 023

Grouping Messages by Time Intervals

可紊 提交于 2019-11-28 02:05:01
问题 I'm currently trying to group messages that are sent out by 1 second time intervals. I'm currently calculating time latency with this: def time_deltas(infile): entries = (line.split() for line in open(INFILE, "r")) ts = {} for e in entries: if " ".join(e[2:5]) == "T out: [O]": ts[e[8]] = e[0] elif " ".join(e[2:5]) == "T in: [A]": in_ts, ref_id = e[0], e[7] out_ts = ts.pop(ref_id, None) yield (float(out_ts),ref_id[1:-1],(float(in_ts)*1000 - float(out_ts)*1000)) INFILE = 'C:/Users/klee

zip_longest without fillvalue

若如初见. 提交于 2019-11-28 01:53:10
I am searching for a middle ground between Python's zip and zip_longest functions (from the itertools module), that exhausts all given iterators, but does not fill in anything. So, for example, it should transpose tuples like so: (11, 12, 13 ), (11, 21, 31, 41), (21, 22, 23, 24), --> (12, 22, 32, 42), (31, 32 ), (13, 23, 43), (41, 42, 43, 44), ( 24, 44) (Spaces added for nicer graphical alignment.) I managed to compose a crude a solution by cleaning out the fillvalue s after zip_longest . def zip_discard(*iterables, sentinel = object()): return map( partial(filter, partial(is_not, sentinel)),

Group consecutive integers and tolerate gaps of 1

人盡茶涼 提交于 2019-11-28 01:02:28
In Python, given a list of sorted integers, I would to group them by consecutive values and tolerate gaps of 1. For instance, given a list my_list : In [66]: my_list Out[66]: [0, 1, 2, 3, 5, 6, 10, 11, 15, 16, 18, 19, 20] I would like the following output: [[0, 1, 2, 3, 5, 6], [10, 11], [15, 16, 18, 19, 20]] Now, if I didn't have to tolerate gaps of 1, I could apply the neat solution explained here : import itertools import operator results = [] for k, g in itertools.groupby(enumerate(my_list), lambda (i,x):i-x): group = map(operator.itemgetter(1), g) results.append(group) Is there a way to