[英]Python find all the different choices from list of list
I want to get all the different choices from a list of lists in python.我想从 python 的列表中获取所有不同的选择。 for example, take a list like,例如,拿一个像这样的列表,
list=[[a,b,c],[a,d,e]]
I just want to select choices from one at each list and get all the possible different selections.我只想从每个列表中的一个中选择 select 并获得所有可能的不同选择。 so above list possible choices look like,所以上面列出的可能选择看起来像,
[a,a]
[a,d]
[a,e]
[b,a]
[b,d]
[b,e]
[c,a]
[c,d]
[c,e]
without using any external library, how can I do this?不使用任何外部库,我该怎么做?
Easiest to implement with a recursive generator.使用递归生成器最容易实现。
def yield_combos(lst):
if lst:
for el in lst[0]:
for combo in yield_combos(lst[1:]):
yield [el] + combo
else:
yield []
lst = [['a','b','c'], ['a','d','e']]
for combo in yield_combos(lst):
print(combo)
If you prefer the output as a list, you can always convert it into one:如果您更喜欢 output 作为列表,您可以随时将其转换为一个:
print(list(yield_combos(lst)))
Minor notes:小提示:
I've called the variable lst
where the question uses list
;我在问题使用list
的地方调用了变量lst
; it's not a good idea to override a builtin, and in this case doing so would prevent the call to list
from working.覆盖内置函数不是一个好主意,在这种情况下,这样做会阻止对list
的调用。
I've set some string values in the caller for sake of a self-contained program (the question had a
in place of 'a'
, etc).为了一个独立的程序,我在调用者中设置了一些字符串值(问题有a
代替'a'
等)。
you could use generators,你可以使用生成器,
perm = [(i,j) for i in list[0] for j in list[1]]
furthermore if you could convert this into a generator object, if you memory concerns,此外,如果您可以将其转换为生成器 object,如果您担心 memory,
perm = ((i,j) for i in list[0] for j in list[1]])
I was told you have n lists and want to permutate all of them, you can use a while loop有人告诉我你有 n 个列表并且想要排列所有列表,你可以使用 while 循环
while(len(list)>1):
a = list.pop()
b = list.pop()
list.append([(i, j) for i in a for j in b])
or make a recursive function.或制作递归 function。 Do note, that the answer will be list[0] and not list itself.请注意,答案将是 list[0] 而不是列表本身。
The simplest way to do this is with two functions最简单的方法是使用两个函数
def funcA(x, arr):
return [(x,c) for c in arr]
def funcB(arr1, arr2):
ans = []
for x in arr1:
ans.extend(funcA(x, arr2))
return ans
Call it whichever way to need:以任何需要的方式调用它:
funcB(your_list[0], your_list[1])
The most pythonic way is with itertools you can simply use product from the itertools library like so:最 Pythonic 的方式是使用 itertools,您可以简单地使用 itertools 库中的产品,如下所示:
my_list = [[a,b,c],[a,d,e]]
# will contain: [(a,a), (a,d)...]
all_combinations = list(itertools.product(*my_list))
The "*" unpacks the list into the lists it contains, its like writing: “*”将列表解包到它包含的列表中,就像这样写:
# will contain: [(a,a), (a,d)...]
list(itertools.product([a,b,c],[a,d,e]))
It is worth mentioning that this works for any number of sublists.值得一提的是,这适用于任意数量的子列表。
my_list = [[a,b,c],[a,d,e], [1,2,3]]
# will contain: [(a,a,1), (a,a,2)...]
all_combinations = list(itertools.product(*my_list))
Hope that helps希望有帮助
l=[['a','b','c'],['a','d','e']]
def combinations(L, tmp=None):
if tmp is None:
tmp = []
if L==[]:
print (tmp)
else:
for i in L[0]:
product(L[1:], tmp+[i])
combinations(l)
Does this help?这有帮助吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.