itertools

How to apply itertools.product to elements of a list of lists?

白昼怎懂夜的黑 提交于 2019-11-27 11:54:20
I have a list of arrays and I would like to get the cartesian product of the elements in the arrays. I will use an example to make this more concrete... itertools.product seems to do the trick but I am stuck in a little detail. arrays = [(-1,+1), (-2,+2), (-3,+3)]; If I do cp = list(itertools.product(arrays)); I get cp = cp0 = [((-1, 1),), ((-2, 2),), ((-3, 3),)] But what I want to get is cp1 = [(-1,-2,-3), (-1,-2,+3), (-1,+2,-3), (-1,+2,+3), ..., (+1,+2,-3), (+1,+2,+3)]. I have tried a few different things: cp = list(itertools.product(itertools.islice(arrays, len(arrays)))); cp = list

Python merging two lists with all possible permutations

允我心安 提交于 2019-11-27 11:50:33
I'm trying to figure out the best way to merge two lists into all possible combinations. So, if I start with two lists like this: list1 = [1, 2] list2 = [3, 4] The resulting list will look like this: [[[1,3], [2,4]], [[1,4], [2,3]]] That is, it basically produces a list of lists, with all the potential combinations between the two. I've been working through itertools, which I'm pretty sure holds the answer, but I can't come up with a way to make it act this way. The closest I came was: list1 = [1, 2, 3, 4] list2 = [5, 6, 7, 8] print list(itertools.product(list1, list2)) Which produced: [(1, 5)

What is the purpose in Python's itertools.repeat?

北战南征 提交于 2019-11-27 11:35:50
Every use I can think of for Python's itertools.repeat() class, I can think of another equally (possibly more) acceptable solution to achieve the same effect. For example: >>> [i for i in itertools.repeat('example', 5)] ['example', 'example', 'example', 'example', 'example'] >>> ['example'] * 5 ['example', 'example', 'example', 'example', 'example'] >>> list(map(str.upper, itertools.repeat('example', 5))) ['EXAMPLE', 'EXAMPLE', 'EXAMPLE', 'EXAMPLE', 'EXAMPLE'] >>> ['example'.upper()] * 5 ['EXAMPLE', 'EXAMPLE', 'EXAMPLE', 'EXAMPLE', 'EXAMPLE'] Is there any case in which it would be the most

What is the difference between chain and chain.from_iterable in itertools?

大兔子大兔子 提交于 2019-11-27 10:41:21
问题 I could not find any valid example on the internet where I can see the difference between them and why to choose one over the other. 回答1: The first takes 0 or more arguments, each an iterable, the second one takes one argument which is expected to produce the iterables: from itertools import chain chain(list1, list2, list3) iterables = [list1, list2, list3] chain.from_iterable(iterables) but iterables can be any iterator that yields the iterables: def gen_iterables(): for i in range(10):

Itertools to generate scrambled combinations

心已入冬 提交于 2019-11-27 08:26:26
问题 What I want to do is obtain all combinations and all unique permutations of each combination. The combinations with replacement function only gets me so far: from itertools import combinations_with_replacement as cwr foo = list(cwr('ACGT', n)) ## n is an integer My intuition on how to move forward is to do something like this: import numpy as np from itertools import permutations as perm bar = [] for x in foo: carp = list(perm(x)) for i in range(len(carp)): for j in range(i+1,len(carp)): if

How to use expand in snakemake when some particular combinations of wildcards are not desired?

為{幸葍}努か 提交于 2019-11-27 08:19:27
问题 Let's suppose that I have the following files, on which I want to apply some processing automatically using snakemake: test_input_C_1.txt test_input_B_2.txt test_input_A_2.txt test_input_A_1.txt The following snakefile uses expand to determine all the potential final results file: rule all: input: expand("test_output_{text}_{num}.txt", text=["A", "B", "C"], num=[1, 2]) rule make_output: input: "test_input_{text}_{num}.txt" output: "test_output_{text}_{num}.txt" shell: """ md5sum {input} >

Itertools Combinations No Repeats: Where rgb is equivelant to rbg etc

懵懂的女人 提交于 2019-11-27 07:34:22
问题 I'm trying to use itertools.combinations to return unique combinations. I've searched through several similar questions but have not been able to find an answer. An example: >>> import itertools >>> e = ['r','g','b','g'] >>> list(itertools.combinations(e,3)) [('r', 'g', 'b'), ('r', 'g', 'g'), ('r', 'b', 'g'), ('g', 'b', 'g')] For my purposes, (r,g,b) is identical to (r,b,g) and so I would want to return only (rgb),(rgg) and (gbg). This is just an illustrative example and I would want to

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

让人想犯罪 __ 提交于 2019-11-27 05:37:03
问题 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

itertools.groupby() not grouping correctly

ぃ、小莉子 提交于 2019-11-27 05:06:59
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 have "1" in that 1 (2nd) position. itertools.groupby collects together contiguous items with the same key.

Replace list of list with “condensed” list of list while maintaining order

馋奶兔 提交于 2019-11-27 04:30:35
I have a list of list as in the code I attached. I want to link each sub list if there are any common values. I then want to replace the list of list with a condensed list of list. Examples: if I have a list [[1,2,3],[3,4]] I want [1,2,3,4] . If I have [[4,3],[1,2,3]] I want [4,3,1,2] . If I have [[1,2,3],[a,b],[3,4],[b,c]] I want [[1,2,3,4],[a,b,c]] or [[a,b,c],[1,2,3,4]] I don't care which one. I am almost there... My problem is when I have a case like [[1,2,3],[10,5],[3,8,5]] I want [1,2,3,10,5,8] but with my current code I get [1,2,3,8,10,5] Here is my code: import itertools a = [1,2,3] b