简体   繁体   English

Python 循环组合两个列表

[英]Python loop to combine two lists

Having trouble with concatenating two lists into another list.将两个列表连接到另一个列表时遇到问题。 Where each list item from one list is combined with every list item of the other list.一个列表中的每个列表项与另一个列表的每个列表项组合在一起。

Example:例子:

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

I am able to get the desired output in a print statement, using a for loop:我可以使用 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

However, I'm having trouble figuring out how to assign this output to a new list但是,我无法弄清楚如何将此 output 分配给新列表

A list comprehension can also be used.也可以使用列表推导。

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

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

Output: Output:

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

Just create a blank list, and append the values to it after each iteration:只需创建一个空白列表,并在每次迭代后 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: Output:

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

This should do what you want:这应该做你想要的:

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)

Make a new list (list_3) and append values to it as tuples.创建一个新列表 (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: output:

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

Or you can use the itertools library and make it a one-liner.或者您可以使用 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))

A few of the answers worked to combine the lists but the following usage of itertools provided an output that was easiest to transfer into a dataframe column一些答案可以组合列表,但以下使用 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