如何迭代两个列表,然后将每对添加到元组中

问题描述 投票:0回答:1

我得到了一份花色列表和一份等级列表。 我想编写一个函数来迭代它们,然后将每一对添加到一个元组中

SUITS = ["Hearts", "Diamonds", "Clubs", "Spades"]
RANKS = [
        "Ace",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "Jack",
        "Queen",
        "King",
]

def create_deck(SUITS, RANKS):
    
    combs_tpl = ()
    for suit in SUITS:
        for rank in RANKS:
            combs_tpl = suit, rank
    return combs_tpl
    

print(create_deck(SUITS, RANKS))
python python-3.x list tuples
1个回答
0
投票

你只有一个元组,它在循环的每次迭代中都被覆盖。 相反,将每个元组附加到 list 并返回它。 例如:

combs_tpls = [] for suit in SUITS: for rank in RANKS: combs_tpls.append((suit, rank)) return combs_tpls
    
© www.soinside.com 2019 - 2024. All rights reserved.