cartesian-product

Combinations with repetition in python, where order MATTERS

我与影子孤独终老i 提交于 2020-07-19 06:54:04
问题 From python's Documentation: https://docs.python.org/2/library/itertools.html#itertools.combinations see combinations_with_replacement: "# combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC" I'd like to use the same function, with the bonus of generating "BA", "CA", and "CB". 回答1: itertools.product is definitely the method you're looking for here. As the documentation states, it is effectively a compact for loop; product(A,B) is equivalent to ((x, y) for x in A for y in B) product

How to get Cartesian product of two iterables when one of them is infinite

戏子无情 提交于 2020-07-06 05:24:08
问题 Let's say I have two iterables, one finite and one infinite: import itertools teams = ['A', 'B', 'C'] steps = itertools.count(0, 100) I was wondering if I can avoid the nested for loop and use one of the infinite iterators from the itertools module like cycle or repeat to get the Cartesian product of these iterables. The loop should be infinite because the stop value for steps is unknown upfront. Expected output: $ python3 test.py A 0 B 0 C 0 A 100 B 100 C 100 A 200 B 200 C 200 etc... Working

How to get Cartesian product of two iterables when one of them is infinite

最后都变了- 提交于 2020-07-06 05:23:51
问题 Let's say I have two iterables, one finite and one infinite: import itertools teams = ['A', 'B', 'C'] steps = itertools.count(0, 100) I was wondering if I can avoid the nested for loop and use one of the infinite iterators from the itertools module like cycle or repeat to get the Cartesian product of these iterables. The loop should be infinite because the stop value for steps is unknown upfront. Expected output: $ python3 test.py A 0 B 0 C 0 A 100 B 100 C 100 A 200 B 200 C 200 etc... Working

How to calculate a Cartesian product of a list with itself [duplicate]

狂风中的少年 提交于 2020-07-03 05:42:18
问题 This question already has answers here : Get the cartesian product of a series of lists? (13 answers) Closed 3 years ago . For example, list = [0, 1, 2] I want a list of all possible 2-combinations: combinations = [(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2)] It seems to me that all the tools in itertools in Python only make one of (1,0) and (0,1), not both, I need both. Any suggestions, other than entering them by hand? 回答1: You are looking for a Cartesian product of that

Idiomatic way to create n-ary cartesian product (combinations of several sets of parameters)

*爱你&永不变心* 提交于 2020-06-27 08:15:11
问题 To create all possible combinations of two sets of parameters and perform an action on them, you can do: setOf(foo, bar, baz).forEach { a -> setOf(0, 1).forEach { b -> /* use a and b */ } } However, if you have (potentially many) more parameters, this quickly turns into a pyramid of doom: setOf(foo, bar, baz).forEach { a -> setOf(0, 1).forEach { b -> setOf(true, false, null).forEach { c -> setOf("Hello,", "World!").forEach { d -> /* use a, b, c and d */ } } } } You could write this similarly