Get the combination of 2 lists

限于喜欢 提交于 2020-06-01 02:20:30

问题


I'm reposting this question because I was told that there is a solution for that in the last post.

I have 2 lists:

list1 = ["foo", "bar", "lorem"]
list2 = ["X", "Y"]

I want to have the possible combinations from these 2 lists, meaning:

[["foo", "bar", "lorem"],
 ["foo", "bar", "loremX"],
 ["foo", "barX", "loremX"],
 ["fooX", "bar", "loremX"],
 ["fooX", "barX", "loremX"],
 ["foo", "barX", "lorem"],
 ["fooX", "barX", "lorem"],
 ["fooX", "bar", "lorem"],

 ["foo", "bar", "lorem"],
 ["foo", "bar", "loremY"],
 ["foo", "barY", "loremY"],
 ["fooY", "bar", "loremY"],
 ["fooY", "barY", "loremY"],
 ["foo", "barY", "lorem"],
 ["fooY", "barY", "lorem"],
 ["fooY", "bar", "lorem"]]

Hope I didn't miss any combination.

Kinda lost with this one.

It probably should be something with itertools.combinations_with_replacement

Thanks.

EDIT

First of all, thanks to @titusarmah99 for a great answer. I managed to take his second-and-very-simple solution and make it generic:

import itertools

list1 = ["foo", "bar", "lorem"]
list2 = ["X", "Y"]
list2new = [""] + list2
newList = [[list1[i]+list2new[j] for j in range(len(list2new))] for i in range(len(list1))]

for index in range(1, len(list2) + 1):
    for c in itertools.product([0,index],repeat=len(list1)):
        tmp = [newList[i][c[i]] for i in range(len(c))]
        print(tmp)


回答1:


The key here is to use rotation. There are many ways to rotate an array, i will be using deque.

from collections import deque

list1 = ["foo", "bar", "lorem"]
list2 = ["X", "Y"]
list2new = [""] + list2
print(list2new) # ['', 'X', 'Y']

newList = [[list1[i]+list2new[j] for j in range(len(list2new))] for i in range(len(list1))]
print(newList)
# [['foo', 'fooX', 'fooY'], ['bar', 'barX', 'barY'], ['lorem', 'loremX', 'loremY']]

d = deque([0,0,0,1,1,1])
for it in range(2*len(list1)):
    tmp = [newList[i][d[i]] for i in range(len(list1))]
    print(tmp) #
    d.rotate(1)
# ['foo', 'bar', 'lorem']
# ['fooX', 'bar', 'lorem']
# ['fooX', 'barX', 'lorem']
# ['fooX', 'barX', 'loremX']
# ['foo', 'barX', 'loremX']
# ['foo', 'bar', 'loremX']

After this we can repeat the process for rest of values in list2.

for x in range(1,len(list2new)):
    d = deque([0]*len(list1)+[x]*len(list1))
    d.rotate(1)
    for it in range(2*len(list1)-1): #
        tmp = [newList[i][d[i]] for i in range(len(list1))]
        print(tmp) #
        d.rotate(1)
# ['fooX', 'bar', 'lorem']
# ['fooX', 'barX', 'lorem']
# ['fooX', 'barX', 'loremX']
# ['foo', 'barX', 'loremX']
# ['foo', 'bar', 'loremX']
# ['fooY', 'bar', 'lorem']
# ['fooY', 'barY', 'lorem']
# ['fooY', 'barY', 'loremY']
# ['foo', 'barY', 'loremY']
# ['foo', 'bar', 'loremY']

This will not contain ['foo', 'bar', 'lorem'] so add it manually.

EDIT: Seems, like you edited the question. To get all possible combinations, just use newList and itertools.product

for c in itertools.product([0,1],repeat=3):
    tmp = [newList[i][c[i]] for i in range(len(c))]
    print(tmp) #



回答2:


Combine each string in list2 with the empty string, then zip the result with list1 and join the strings.

from itertools import product

for item in list2:
    for suffixes in product(['', item], repeat=3):
        print([x + s for x, s in zip(list1, suffixes)])

Output:

['foo', 'bar', 'lorem']
['foo', 'bar', 'loremX']
['foo', 'barX', 'lorem']
['foo', 'barX', 'loremX']
['fooX', 'bar', 'lorem']
['fooX', 'bar', 'loremX']
['fooX', 'barX', 'lorem']
['fooX', 'barX', 'loremX']
['foo', 'bar', 'lorem']
['foo', 'bar', 'loremY']
['foo', 'barY', 'lorem']
['foo', 'barY', 'loremY']
['fooY', 'bar', 'lorem']
['fooY', 'bar', 'loremY']
['fooY', 'barY', 'lorem']
['fooY', 'barY', 'loremY']

Here I'm printing to show the lists on one line each, but you could easily modify this to append them to another list.



来源:https://stackoverflow.com/questions/61605028/get-the-combination-of-2-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!