简体   繁体   English

如何在两个列表之间交替 output?

[英]How to alternate output between two lists?

I am trying to output which card a player gets as the card is being handed out.我正在尝试 output 玩家在分发卡片时获得哪张卡片。 My idea was to output each card by alternating from the two lists.我的想法是 output 每张卡通过交替从两个列表。 I have included an example below.我在下面提供了一个示例。

Ex.
List1 = [Banana,Cherry,Orange,Apple,Tomato]
List2 = [Blue,Red,Orange,Yellow,Grey,Purple]

Output:

    Banana
    Blue
    Cherry
    Red
    Orange
    Yellow...

This is what I have so far.这就是我到目前为止所拥有的。

for card in player_A:
    print("Player A is dealt= ",card,"")
for card in Player_B:
    print("Player B is dealt=",card,"")

How can I go about doing that?我怎么能 go 这样做呢?

Use zip to alternate between the two lists.使用zip在两个列表之间交替。

for a, b in zip(List1, List2):
  print(f'Player A is dealt= {a}')
  print(f'Playber B is dealt= {b}')

Output Output

Player A is dealt= Banana
Playber B is dealt= Blue
Player A is dealt= Cherry
Playber B is dealt= Red
Player A is dealt= Orange
Playber B is dealt= Orange
Player A is dealt= Apple
Playber B is dealt= Yellow
Player A is dealt= Tomato
Playber B is dealt= Grey

You can just do你可以做

for i in range(len(List1)):
    print("Player A is dealt= ", List1[i], "")
    print("Player B is dealt= ", List2[i], "")

暂无
暂无

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

相关问题 如何使用协程在两个列表之间交替打印? - How to use coroutines to alternate printing between two lists? Python:如何在两个问题之间交替选择以检索两个不同列表的用户输入? - Python: How to alternate between two questions to retrieve user inputs for two different lists? 如何找到两个列表之间的匹配并根据匹配写入输出? - How to find the match between two lists and write the output based on matches? 如何将两个列表作为输入并从其他两个列表中进行算术运算得到输出? - How to take two lists as an input and get the output from another two lists with arithmetic operations in between? 如何“交替”显示两个列表的内容? - How can I “alternate” display the content of two lists? 使用子列表的元素创建列表,这些子列表在来自其他两个列表的子列表中的元素之间交替 - create list with elements which are sublists that alternate between elements in sublists from two other lists 这个 Python 查询如何在两个不同的结果之间交替? - How is it possible for this Python query to alternate between two different results? 如何在两个列表之间进行 grep 并输出“search_term”的密钥对:“item_found” - How to grep between two lists and output key pairs of 'search_term' : 'item_found' 球员之间如何轮换? - How to alternate between players? 熊猫:两列字符串列表之间的输出差异 - pandas:output differences between two columns of lists of strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM