在python中使用函数参数命名的列表

use*_*061 1 python arguments list

我觉得这可能是我应该知道的,但我现在想不到它.我正在尝试获取一个函数来构建一个列表,其中列表的名称是函数中给出的参数;

例如

def make_hand(deck, handname):
    handname = []
    for c in range(5):
        handname.append(deck.pop())
    return handname

    # deck being a list containing all the cards in a deck of cards earlier
Run Code Online (Sandbox Code Playgroud)

问题在于,当我希望在创建手时用户输入的任何名称时,都会创建一个名为handname的列表.

有人可以帮忙吗?谢谢

Kiv*_*Kiv 6

您可以保留一个字典,其中键是手的名称,值是列表.

然后你可以说字典[handname]来访问特定的手.沿着:

hands = {} # Create a new dictionary to hold the hands.
hands["flush"] = make_hand(deck) # Generate some hands using your function.
hands["straight"] = make_hand(deck) # Generate another hand with a different name.
print hands["flush"] # Access the hand later.
Run Code Online (Sandbox Code Playgroud)