itertools

getting all possible combinations of a list in a form of sublists

不打扰是莪最后的温柔 提交于 2020-01-24 04:08:26
问题 I wonder if someone can help with the following task: What is the way to get all combinations a list can be split into sublists, when order does not matter? Let's say I have a list of 4 items: import itertools as it a = [1, 2, 3, 4] print(list(it.combinations(a, 2))) That will give me a list of 6 possible pairs: [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] How to make (out of it? or any other way) a set of lists that contain original [1, 2, 3, 4] sequence in any order? So for this example

Python List Group by Date

被刻印的时光 ゝ 提交于 2020-01-21 03:34:10
问题 Say I have a list looks like this: [(datetime.datetime(2013, 8, 8, 1, 20, 15), 2060), (datetime.datetime(2013, 8, 9, 1, 6, 14), 2055), (datetime.datetime(2013, 8, 9, 1, 21, 1), 2050), (datetime.datetime(2013, 8, 10, 1, 5, 49), 2050), (datetime.datetime(2013, 8, 10, 1, 19, 51), 2050), (datetime.datetime(2013, 8, 11, 2, 4, 53), 2050), (datetime.datetime(2013, 8, 12, 0, 29, 45), 2050), (datetime.datetime(2013, 8, 12, 0, 44, 13), 2050), (datetime.datetime(2013, 8, 13, 0, 34, 13), 2050), (datetime

How can I avoid nested tuple unpacking when enumerating zipped lists?

荒凉一梦 提交于 2020-01-14 05:46:06
问题 How can I avoid using nested tuple unpacking when enumerating a list of tuples like this? for i, (x, y) in enumerate(zip("1234", "ABCD")): # do stuff 回答1: Use itertools.count to avoid nested tuple unpacking: from itertools import count for i, x, y in zip(count(), "1234", "ABCD"): # do stuff 来源: https://stackoverflow.com/questions/11968729/how-can-i-avoid-nested-tuple-unpacking-when-enumerating-zipped-lists

Count consecutive ones in a dataframe and get indices where this occurs

梦想的初衷 提交于 2020-01-13 16:28:23
问题 I have a pandas.DataFrame with integer column names, which has zeroes and ones. An example of the input: 12 13 14 15 1 0 0 1 0 2 0 0 1 1 3 1 0 0 1 4 1 1 0 1 5 1 1 1 0 6 0 0 1 0 7 0 0 1 1 8 1 1 0 1 9 0 0 1 1 10 0 0 1 1 11 1 1 0 1 12 1 1 1 1 13 1 1 1 1 14 1 0 1 1 15 0 0 1 1 I need to count all consecutive ones which has a length/sum which is >=2, iterating through columns and returning also indices where an array of the consecutive ones occurs (start, end). The preferred output would be a 3D

Count consecutive ones in a dataframe and get indices where this occurs

偶尔善良 提交于 2020-01-13 16:27:53
问题 I have a pandas.DataFrame with integer column names, which has zeroes and ones. An example of the input: 12 13 14 15 1 0 0 1 0 2 0 0 1 1 3 1 0 0 1 4 1 1 0 1 5 1 1 1 0 6 0 0 1 0 7 0 0 1 1 8 1 1 0 1 9 0 0 1 1 10 0 0 1 1 11 1 1 0 1 12 1 1 1 1 13 1 1 1 1 14 1 0 1 1 15 0 0 1 1 I need to count all consecutive ones which has a length/sum which is >=2, iterating through columns and returning also indices where an array of the consecutive ones occurs (start, end). The preferred output would be a 3D

How do I reverse an itertools.chain object?

删除回忆录丶 提交于 2020-01-12 14:51:11
问题 My function creates a chain of generators: def bar(num): import itertools some_sequence = (x*1.5 for x in range(num)) some_other_sequence = (x*2.6 for x in range(num)) chained = itertools.chain(some_sequence, some_other_sequence) return chained My function sometimes needs to return chained in reversed order. Conceptually, the following is what I would like to be able to do: if num < 0: return reversed(chained) return chained Unfortunately: >>> reversed(chained) TypeError: argument to reversed

How do I reverse an itertools.chain object?

扶醉桌前 提交于 2020-01-12 14:50:47
问题 My function creates a chain of generators: def bar(num): import itertools some_sequence = (x*1.5 for x in range(num)) some_other_sequence = (x*2.6 for x in range(num)) chained = itertools.chain(some_sequence, some_other_sequence) return chained My function sometimes needs to return chained in reversed order. Conceptually, the following is what I would like to be able to do: if num < 0: return reversed(chained) return chained Unfortunately: >>> reversed(chained) TypeError: argument to reversed

How do I reverse an itertools.chain object?

末鹿安然 提交于 2020-01-12 14:50:11
问题 My function creates a chain of generators: def bar(num): import itertools some_sequence = (x*1.5 for x in range(num)) some_other_sequence = (x*2.6 for x in range(num)) chained = itertools.chain(some_sequence, some_other_sequence) return chained My function sometimes needs to return chained in reversed order. Conceptually, the following is what I would like to be able to do: if num < 0: return reversed(chained) return chained Unfortunately: >>> reversed(chained) TypeError: argument to reversed

itertools.groupby() not grouping correctly

跟風遠走 提交于 2020-01-08 17:43:13
问题 I have this data: self.data = [(1, 1, 5.0), (1, 2, 3.0), (1, 3, 4.0), (2, 1, 4.0), (2, 2, 2.0)] When I run this code: for mid, group in itertools.groupby(self.data, key=operator.itemgetter(0)): for list(group) I get: [(1, 1, 5.0), (1, 2, 3.0), (1, 3, 4.0)] which is what I want. But if I use 1 instead of 0 for mid, group in itertools.groupby(self.data, key=operator.itemgetter(1)): to group by the second number in the tuples, I only get: [(1, 1, 5.0)] even though there are other tuples that

itertools product to generate all possible strings of size 3

醉酒当歌 提交于 2020-01-06 13:06:45
问题 Input: pos_1= 'AVNMHDRW' pos_2= 'KNTHDYBW' pos_3= 'KVNGSDRB' Trying to find all possible triplets using one item from pos_1, one from pos_2, and one from pos_3 I'm trying to figure out how to use itertools.product(*) but I'm a little confused Ultimately, I want to create a list (or generator object) of all the different possibilities by taking one from pos_1 then one from pos_2 and then one from pos_3 Example output: 'AKK','ANV','WWB' pos_1 stands for position one and so on for pos_2 and pos