Python 3.x 组合两个列表的元素

Python 3.x 组合两个列表的元素,python-3.x,Python 3.x,我有两份清单: lst1 = ['a', 'b'] lst2 = ['c', 'd', 'e'] 我想做这样的组合: [['a', 'c'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'], ['b', 'e']] 请帮我做这个。感谢您导入itertools >>> import itertools >>> lst1 = ['a', 'b'] >>> lst2 = ['c', 'd', 'e

我有两份清单:

lst1 = ['a', 'b']
lst2 = ['c', 'd', 'e']
我想做这样的组合:

[['a', 'c'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'], ['b', 'e']]
请帮我做这个。感谢您导入itertools
>>> import itertools
>>> lst1 = ['a', 'b']
>>> lst2 = ['c', 'd', 'e']
>>> itertools.product(lst1, lst2)
<itertools.product object at 0x7f3571488280>
>>> list(itertools.product(lst1, lst2))
[('a', 'c'), ('a', 'd'), ('a', 'e'), ('b', 'c'), ('b', 'd'), ('b', 'e')]
>>> x = list(itertools.product(lst1, lst2))
>>> [list(y) for y in x]
[['a', 'c'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'], ['b', 'e']]
>>>
>>>lst1=['a','b'] >>>lst2=['c','d','e'] >>>itertools.产品(lst1、lst2) >>>列表(itertools.product(lst1、lst2)) [('a','c'),('a','d'),('a','e'),('b','c'),('b','d'),('b','e')] >>>x=列表(itertools.product(lst1、lst2)) >>>[列表(y)表示x中的y] [a',c'],[a',d'],[a',e'],[b',c'],[b',d'],[b',e'] >>>
我给大家留下一个不使用库的例子

脚本:

lst1 = ['a', 'b']
lst2 = ['c', 'd', 'e']
lst3 = []

for item_lst1 in lst1:
    for item_lst2 in lst2:
        lst3.append([item_lst1, item_lst2])

print(lst3)
输出:

[['a', 'c'], ['a', 'd'], ['a', 'e'], ['b', 'c'], ['b', 'd'], ['b', 'e']]

这可能对你有用

   list1 = ['a', 'b']
   list2 = ['c', 'd', 'e']
    
   req_list = [[x, y] for x in list1 for y in list2]

这种类型的组合称为笛卡尔积或叉积。

到目前为止您尝试过什么?您是否需要一份清单,还是可以接受?