繁体   English   中英

Python 循环组合两个列表

[英]Python loop to combine two lists

将两个列表连接到另一个列表时遇到问题。 一个列表中的每个列表项与另一个列表的每个列表项组合在一起。

例子:

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three'] 

我可以使用 for 循环在打印语句中获得所需的 output :

for x in list_1:
    for y in list_2:
        print(x,y)

output:
a one
a two
a three
b one
b two
b three
c one
c two
c three

但是,我无法弄清楚如何将此 output 分配给新列表

也可以使用列表推导。

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three'] 

print([(a, b) for a in list_1 for b in list_2])

Output:

[('a', 'one'), ('a', 'two'), ('a', 'three'), ('b', 'one'), ('b', 'two'), ('b', 'three'), ('c', 'one'), ('c', 'two'), ('c', 'three')]

只需创建一个空白列表,并在每次迭代后 append 为其分配值:

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three']
list_3 = []
for x in list_1:
    for y in list_2:
        # In 1 line: list_3.extend([x, y]) 
        list_3.append(x)
        list_3.append(y)

print(list_3)

Output:

['a', 'one', 'a', 'two', 'a', 'three', 'b', 'one', 'b', 'two', 'b', 'three', 'c', 'one', 'c', 'two', 'c', 'three']

这应该做你想要的:

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three']

list_3 = []
for x in list_1:
    for y in list_2:
        list_3 += [x,y]

print(list_3)

创建一个新列表 (list_3) 和 append 值作为元组。

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three']
list_3 = []

for i, in list_1: 
    for j in list_2:
        print(i,j)
        k = i,j
        list_3.append(k)
>>> [(i, inner_i )for i in ['a','b','c'] for inner_i in ['one', 'two', 'three']] [('a', 'one'), ('a', 'two'), ('a', 'three'), ('b', 'one'), ('b', 'two'), ('b', 'three'), ('c', 'one'), ('c', 'two'), ('c', 'three')]
list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three'] 
list_3 = [[a, b] for a in list_1 for b in list_2]

print(list_3)

output:

[['a', 'one'], ['a', 'two'], ['a', 'three'], ['b', 'one'], ['b', 'two'], ['b', 'three'], ['c', 'one'], ['c', 'two'], ['c', 'three']]

或者您可以使用 itertools 库并使其成为单线。

import itertools

list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three'] 

list_3 = itertools.product(list_1, list_2)

print(list(list_3))

一些答案可以组合列表,但以下使用 itertools 提供了 output 最容易转移到 dataframe 列

from itertools import product
list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three']
list_3 = ['you', 'and', 'me']

result = [f'{a} {b} {c}' for a, b, c in product(list_1, list_2, list_3)]

Output:

['a one you',
 'a one and',
 'a one me',
 'a two you',
 'a two and',
 'a two me',
 'a three you',
 'a three and',
 'a three me',
 'b one you',
 'b one and',
 'b one me',
 'b two you',
 'b two and',
 'b two me',
 'b three you',
 'b three and',
 'b three me',
 'c one you',
 'c one and',
 'c one me',
 'c two you',
 'c two and',
 'c two me',
 'c three you',
 'c three and',
 'c three me']
list_1 = ['a','b','c']
list_2 = ['one', 'two', 'three']

#Initializing an empty list, then inserting it into the empty list
list_3 = []

#Loop
for x in list_1:
    for y in list_2:
        list_3 += [x,y]

#Print list_3
print(list_3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM