itertools

Conditional Cartesian product of lists in itertools

这一生的挚爱 提交于 2020-01-02 10:28:13
问题 I have four lists: LISTA = ['A1', 'A2'] LISTB = ['B1_C', 'B2_D'] LISTC = ['C1', 'C2'] LISTD = ['D1', 'D2'] I'd like to get the Cartesian product of LISTA and LISTB , and then depending on the value of B, I'd like to add either the product of C, or the product of D. (A1 B1_C C1) (A1 B1_C C2) (A2 B1_C C1) (A2 B1_C C2) (A1 B2_D D1) (A1 B2_D D2) (A2 B2_D D1) (A2 B2_D D2) I can get the first part with itertools.product(LISTA, LISTB) , but I've been looking through itertools for how to achieve the

Conditional Cartesian product of lists in itertools

谁说胖子不能爱 提交于 2020-01-02 10:28:06
问题 I have four lists: LISTA = ['A1', 'A2'] LISTB = ['B1_C', 'B2_D'] LISTC = ['C1', 'C2'] LISTD = ['D1', 'D2'] I'd like to get the Cartesian product of LISTA and LISTB , and then depending on the value of B, I'd like to add either the product of C, or the product of D. (A1 B1_C C1) (A1 B1_C C2) (A2 B1_C C1) (A2 B1_C C2) (A1 B2_D D1) (A1 B2_D D2) (A2 B2_D D1) (A2 B2_D D2) I can get the first part with itertools.product(LISTA, LISTB) , but I've been looking through itertools for how to achieve the

How to create all combinations column wise for multiple variables in pandas?

梦想的初衷 提交于 2020-01-02 10:19:50
问题 For a given range for n variables. I have taken n=3 as an example. A : [1,3] B: [5,10,12] C: [100,113] Note the values in the above range can be float as well. How can we create a dataframe where every column represent a unique combination of the input variables? c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 a 1 1 1 3 3 3 1 1 1 3 3 3 b 5 10 12 5 10 12 5 10 12 5 10 12 c 100 100 100 100 100 100 113 113 113 113 113 113 回答1: Using Itertools.product I can make all combinations of a list, afterwards you

More efficient way to get integer permutations?

霸气de小男生 提交于 2020-01-02 00:54:09
问题 I can get integer permutations like this: myInt = 123456789 l = itertools.permutations(str(myInt)) [int(''.join(x)) for x in l] Is there a more efficient way to get integer permutations in Python, skipping the overhead of creating a string, then joining the generated tuples? Timing it, the tuple-joining process makes this 3x longer than list(l) . added supporting information myInt =123456789 def v1(i): #timeit gives 258ms l = itertools.permutations(str(i)) return [int(''.join(x)) for x in l]

itertools: Cartesian product of permutations

ⅰ亾dé卋堺 提交于 2020-01-01 15:36:09
问题 Using pythons itertools , I'd like to create an iterator over the outer product of all permutations of a bunch of lists. An explicit example: import itertools A = [1,2,3] B = [4,5] C = [6,7] for x in itertools.product(itertools.permutations(A),itertools.permutations(B),itertools.permutations(C)): print x While this works, I'd like to generalize it to an arbitrary list of lists. I tried: for x in itertools.product(map(itertools.permutations,[A,B,C])): print x but it did not do what I intended.

itertools: Cartesian product of permutations

限于喜欢 提交于 2020-01-01 15:36:04
问题 Using pythons itertools , I'd like to create an iterator over the outer product of all permutations of a bunch of lists. An explicit example: import itertools A = [1,2,3] B = [4,5] C = [6,7] for x in itertools.product(itertools.permutations(A),itertools.permutations(B),itertools.permutations(C)): print x While this works, I'd like to generalize it to an arbitrary list of lists. I tried: for x in itertools.product(map(itertools.permutations,[A,B,C])): print x but it did not do what I intended.

My IDLE does not recognize itertools.izip() as a function

谁说我不能喝 提交于 2020-01-01 08:49:18
问题 >>> itertools.izip('ABCD', 'xy') Traceback (most recent call last): File "<pyshell#55>", line 1, in <module> itertools.izip('ABCD', 'xy') AttributeError: 'module' object has no attribute 'izip' 回答1: In Python 3, there is no izip function in the itertools module because the builtin zip function (which doesn't require any imports to access) now behaves like itertools.izip did in Python 2. So, to make your code work, just use zip instead of itertools.izip . You also mentioned an issue with

Ordered subsets test

a 夏天 提交于 2020-01-01 05:42:06
问题 I want to test if an ordered set is a subset of a bigger ordered set. I used tuples and itertools.combinations : def subset_test(a, b): return a in itertools.combinations(b, len(a)) For instance, >>> subset_test((0, 1, 2), (0, 3, 1, 4, 2)) True >>> subset_test((0, 1, 2), (0, 3, 2, 4, 1)) False It works, but is slow when I test big tuples. 回答1: You can simply use an iterator to keep track of the position in B >>> A = (0, 1, 2) >>> B = (0, 3, 1, 4, 2) >>> b_iter = iter(B) >>> all(a in b_iter

Concatenate tuples using sum()

佐手、 提交于 2020-01-01 01:17:13
问题 From this post I learned that you can concatenate tuples with: >>> tuples = (('hello',), ('these', 'are'), ('my', 'tuples!')) >>> sum(tuples, ()) ('hello', 'these', 'are', 'my', 'tuples!') Which looks pretty nice. But why does this work? And, is this optimum, or is there something from itertools that would be preferable to this construct? 回答1: the addition operator concatenates tuples in python: ('a', 'b')+('c', 'd') Out[34]: ('a', 'b', 'c', 'd') From the docstring of sum : Return the sum of

Can this python code be more efficient?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-31 01:56:31
问题 I have written some code to find how many substrings of a string are anagram pairs. The function to find anagram(anagramSolution) is of complexity O(N). The substring function has complexity less than N square. But, this code here is the problem. Can it be more optimized? for i in range(T): x = raw_input() alist = get_all_substrings(x) for k, j in itertools.combinations(alist,2): if(len(k) == len(j)): if(anagramSolution(k,j)): counter +=1 counterlist.append(counter) counter = 0 The alist can