【问题标题】:How do I concatenate two lists together but with different frequencies [duplicate]如何将两个列表连接在一起但频率不同[重复]
【发布时间】:2020-03-08 01:46:35
【问题描述】:

我有两个列表,比如说:

list1 = ["red", "blue", "yellow"]
list2 = ["frog", "lion", "tiger", "ant", "shrew", "bee"]

我想循环连接两个列表,但我希望 list1 的每个值与 list2 的每个值连接,然后再转到 list1 的下一个值。即结果会说红蛙、红狮、红虎、红蚂蚁、红鼩、红蜜蜂、蓝蛙等。

我不知道如何错开循环。

【问题讨论】:

  • 看看嵌套循环。这应该是一个很好的提示,可以让您继续前进。如果您需要解决方案,只需发表另一条评论。嵌套循环教程:tutorialspoint.com/python/python_nested_loops.htm
  • 我想,itertools.product(list1, list2) 就是你要找的东西

标签: python function loops concatenation


【解决方案1】:
listColourAnimal = []
for colour in list1:
    for animal in list2:
        listColourAnimal.append(colour + ' ' + animal)

print(listColourAnimal)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 2020-09-11
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多