组合和循环列表python

时间:2021-03-24 14:01:59
first_name = ["Johnny", "Joseph", "Mary"]
third_name = ["Mendez", "Nash", "Johanson"]
second_name = ["John", "Allen", "Ann"]


for i in first_name:
    for x in second_name:
        for y in third_name:
            names = i + x + y

how can we make the output:

我们怎样才能输出:

[Johnny John Mendez, Joseph Allen Nash, Mary Ann Johanson]

because the when I loop through all the lists I get jumbled names. How can we make the order of the names. first_name second_name third_name?

因为当我遍历所有列表时,我得到了混乱的名字。我们如何制作名称的顺序。 first_name second_name third_name?

3 个解决方案

#1


Use zip and map:

使用zip和地图:

map(" ".join, zip(first_names, second_names, third_names))

Example in the REPL:

REPL中的示例:

>>> map(" ".join, zip(["A", "B", "C"], ["a", "b", "c"], ["1", "2", "3"]))
['A a 1', 'B b 2', 'C c 3']

and then you can create a function you might use elsewhere:

然后你可以创建一个你可能在别处使用的函数:

def make_names(first_names, second_names, third_names):
    return map(" ".join, zip(first_names, second_names, third_names))

or even more general not hardcoding exactly three name parts:

甚至更一般的是不硬编码正好三个名称部分:

def make_names(*args):
    return map(" ".join, zip(*args))

REPL example:

>>> make_names(["a"])
['a']
>>> make_names(["a"], ["b"])
['a b']
>>> make_names(["a"], ["b"], ["c"], ["d"])
['a b c d']
>>> make_names(["Johnny", "Joseph", "Mary"], ["Mendez", "Nash", "Johanson"], ["John", "Allen", "Ann"])
['Johnny Mendez John', 'Joseph Nash Allen', 'Mary Johanson Ann']

#2


Use zip():

for first, second, third in zip(first_name, second_name, third_name):
    print "{} {} {}".format(first, second, third)

The zip function iterates through the three lists at the same time, and takes the corresponding element from each list on each iteration.

zip函数同时遍历三个列表,并在每次迭代时从每个列表中获取相应的元素。

#3


This will work if you have lists of unequal size

如果您有不等大小的列表,这将有效

In [18]: import itertools
In [19]: [x for x in itertools.chain.from_iterable(itertools.izip_longest(first_name,second_name,third_name)) if x]
Out[19]:["Johnny","John Mendez","Joseph", "Allen", "Nash", "Mary", "Ann", "Johanson"] 

#1


Use zip and map:

使用zip和地图:

map(" ".join, zip(first_names, second_names, third_names))

Example in the REPL:

REPL中的示例:

>>> map(" ".join, zip(["A", "B", "C"], ["a", "b", "c"], ["1", "2", "3"]))
['A a 1', 'B b 2', 'C c 3']

and then you can create a function you might use elsewhere:

然后你可以创建一个你可能在别处使用的函数:

def make_names(first_names, second_names, third_names):
    return map(" ".join, zip(first_names, second_names, third_names))

or even more general not hardcoding exactly three name parts:

甚至更一般的是不硬编码正好三个名称部分:

def make_names(*args):
    return map(" ".join, zip(*args))

REPL example:

>>> make_names(["a"])
['a']
>>> make_names(["a"], ["b"])
['a b']
>>> make_names(["a"], ["b"], ["c"], ["d"])
['a b c d']
>>> make_names(["Johnny", "Joseph", "Mary"], ["Mendez", "Nash", "Johanson"], ["John", "Allen", "Ann"])
['Johnny Mendez John', 'Joseph Nash Allen', 'Mary Johanson Ann']

#2


Use zip():

for first, second, third in zip(first_name, second_name, third_name):
    print "{} {} {}".format(first, second, third)

The zip function iterates through the three lists at the same time, and takes the corresponding element from each list on each iteration.

zip函数同时遍历三个列表,并在每次迭代时从每个列表中获取相应的元素。

#3


This will work if you have lists of unequal size

如果您有不等大小的列表,这将有效

In [18]: import itertools
In [19]: [x for x in itertools.chain.from_iterable(itertools.izip_longest(first_name,second_name,third_name)) if x]
Out[19]:["Johnny","John Mendez","Joseph", "Allen", "Nash", "Mary", "Ann", "Johanson"]