获取2个列表的组合 [英] Get the combination of 2 lists

查看:53
本文介绍了获取2个列表的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重新发布此问题,因为在上一篇帖子中有人告诉我有解决方案.

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

我有2个列表:

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迷失了这个.

Kinda lost with this one.

itertools.combinations_with_replacement可能应该包含一些东西

It probably should be something with itertools.combinations_with_replacement

谢谢.

编辑

首先,感谢@ titusarmah99的出色回答. 我设法采用了他的第二个非常简单的解决方案并使之通用:

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)

推荐答案

此处的关键是使用旋转. 旋转阵列的方法很多,我将使用deque.

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']

此后,我们可以为list2中的其余值重复该过程.

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']

其中将不包含['foo', 'bar', 'lorem'],因此请手动添加.

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

似乎就像您编辑问题一样.要获得所有可能的组合,只需使用newListitertools.product

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个列表的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆