combinations between two lists?

2019-01-02 18:02发布

It’s been a while and I’m having trouble wrapping my head around a algorithm I’m try to make. Basically, I have two lists and want to get all the combinations of the two lists.

I might not be explaining it correct so here’s a example.

name = 'a', 'b'
number = 1, 2

the output in this case would be:

1.  A1 B2
2.  B1 A2

The tricky part is I might have more items in the “name” variable than items in the “number” variable(number will always be equal to or less than the name variable).

I’m confused how to do all the combinations (nested for loop?) and even more confused on the logic to shift the items in the name variable in the event that there are more items in name than they are in the number list.

I’m not the best programmer but think I can give it a shot if someone can help me clarify the logic/algoriythm to achieve this. So I have just been stuck on nested for loops.

Update:

Here's the output with 3 variables and 2 numbers:

name = 'a', 'b', 'c'
number = 1, 2

output:

1.  A1 B2
2.  B1 A2
3.  A1 C2
4.  C1 A2
5.  B1 C2
6.  C1 B2

8条回答
美炸的是我
2楼-- · 2019-01-02 18:17

May be simpler than the simplest one above:

>>> a = ["foo", "bar"]
>>> b = [1, 2, 3]
>>> [(x,y) for x in a for y in b]
[('foo', 1), ('foo', 2), ('foo', 3), ('bar', 1), ('bar', 2), ('bar', 3)]

without any import

查看更多
素衣白纱
3楼-- · 2019-01-02 18:17

You might want to try a one line list comprehension:

>>> [name+number for name in 'ab' for number in '12']
['a1', 'a2', 'b1', 'b2']
>>> [name+number for name in 'abc' for number in '12']
['a1', 'a2', 'b1', 'b2', 'c1', 'c2']
查看更多
唯独是你
4楼-- · 2019-01-02 18:18

a tiny improvement for the answer from interjay, to make the result as a flatten list.

>>> list3 = [zip(x,list2) for x in itertools.permutations(list1,len(list2))]
>>> import itertools
>>> chain = itertools.chain(*list3)
>>> list4 = list(chain)
[('a', 1), ('b', 2), ('a', 1), ('c', 2), ('b', 1), ('a', 2), ('b', 1), ('c', 2), ('c', 1), ('a', 2), ('c', 1), ('b', 2)]

reference from this link

查看更多
伤终究还是伤i
5楼-- · 2019-01-02 18:20

Without itertools

[(list1[i], list2[j]) for i in xrange(len(list1)) for j in xrange(len(list2))]
查看更多
永恒的永恒
6楼-- · 2019-01-02 18:26

The simplest way is to use itertools.product:

a = ["foo", "melon"]
b = [True, False]
c = list(itertools.product(a, b))
>> [("foo", True), ("foo", False), ("melon", True), ("melon", False)]
查看更多
不流泪的眼
7楼-- · 2019-01-02 18:30

I was looking for a list multiplied by itself with only unique combinations, which is provided as this function.

import itertools
itertools.combinations(list, n_times)

Here as an excerpt from the Python docs on itertools That might help you find what your looking for.

Combinatoric generators:

Iterator                                 | Results
-----------------------------------------+----------------------------------------
product(p, q, ... [repeat=1])            | cartesian product, equivalent to a 
                                         |   nested for-loop
-----------------------------------------+----------------------------------------
permutations(p[, r])                     | r-length tuples, all possible 
                                         |   orderings, no repeated elements
-----------------------------------------+----------------------------------------
combinations(p, r)                       | r-length tuples, in sorted order, no 
                                         |   repeated elements
-----------------------------------------+----------------------------------------
combinations_with_replacement(p, r)      | r-length tuples, in sorted order, 
                                         | with repeated elements
-----------------------------------------+----------------------------------------
product('ABCD', repeat=2)                | AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD
permutations('ABCD', 2)                  | AB AC AD BA BC BD CA CB CD DA DB DC
combinations('ABCD', 2)                  | AB AC AD BC BD CD
combinations_with_replacement('ABCD', 2) | AA AB AC AD BB BC BD CC CD DD
查看更多
登录 后发表回答