pop() 从字典中的多个列表中删除值

pop() removing value from multiple lists in dictionary


def create_deck():
    """
    A function that creates a full 52-card deck\n
    :return: dict
    """
    suits = ["hearts", "diamonds", "spades", "clubs"]
    cards = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]

    # Creates a full deck of cards with dictionary comprehension
    # using the indices from the list suits as keys
    deck_of_cards = {suit: cards for suit in suits}
    return deck_of_cards


def select_card(deck_dictionary):
    """
    A function that selects a single card from a 52-card deck.
    Return the card face name, number and value\n
    :return: tuple
    """
    import random
    deck = deck_dictionary
    random_suit = random.choice([x for x in deck])
    # random_card = random.choice(range(len(deck[random_suit]) - 1))
    # card = deck[random_suit].pop([random_card])
    # return random_suit, card
    random.shuffle(deck[random_suit])
    card = deck[random_suit].pop(0)
    return card, random_suit, deck


deck_obj = create_deck()
print(deck_obj)
print(select_card(deck_obj))
print(select_card(deck_obj))

输出 原始卡组

{'hearts': ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'], 'diamonds': ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'], 'spades': ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'], 'clubs': ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']}

第一关select_card()

('Q', 'hearts', {'hearts': ['7', '10', '6', '9', '4', '2', 'K', '8', 'J', '5', 'A', '3'], 'diamonds': ['7', '10', '6', '9', '4', '2', 'K', '8', 'J', '5', 'A', '3'], 'spades': ['7', '10', '6', '9', '4', '2', 'K', '8', 'J', '5', 'A', '3'], 'clubs': ['7', '10', '6', '9', '4', '2', 'K', '8', 'J', '5', 'A', '3']})

第二次通过 select_card()

('5', 'hearts', {'hearts': ['10', '9', '6', '4', '7', 'A', '8', 'J', 'K', '3', '2'], 'diamonds': ['10', '9', '6', '4', '7', 'A', '8', 'J', 'K', '3', '2'], 'spades': ['10', '9', '6', '4', '7', 'A', '8', 'J', 'K', '3', '2'], 'clubs': ['10', '9', '6', '4', '7', 'A', '8', 'J', 'K', '3', '2']})

如果您看到我正在删除每个列表中的 'card' 变量,而不是在选定的 'random_suit' 列表中删除它。

我已经在这几个小时了。 returns 目前仅供测试。

我也试过:

def create_deck():
    """
    A function that creates a full 52-card deck\n
    :return: dict
    """
    royals = {"J": 10, "Q": 10, "K": 10}
    suits = ["hearts", "diamonds", "spades", "clubs"]
    cards = list(range(2, 11))
    cards += royals.items()

    # Creates a full deck of cards with dictionary comprehension
    # using the indices from the list suits as keys
    # and set all values with the cards variable - 2 through to 10 and adding royal cards
    deck_of_cards = {suit: cards for suit in suits}
    return deck_of_cards
def select_card(deck_dictionary):
    """
    A function that selects a single card from a 52-card deck.
    Return the card face name, number and value\n
    :return: tuple
    """
    import random
    deck = deck_dictionary
    random_suit = random.choice([x for x in deck])
    random_card = random.choice(range(len(deck[random_suit]) - 1))
    if random_card > 8:
        card = list(deck[random_suit][random_card])
        (deck[random_suit])
        return deck
        return random_suit, card
    else:
        card = deck[random_suit][random_card]
        del deck[random_suit][random_card]
        return deck
        return random_suit, card

这与上面的代码具有相同的结果,删除了每个列表中不是预期的元素 'random_suit'。

问题是每套花色都有相同的卡片列表 - 相同的实例 - 然后您正在修改(排序和删除)。您可以索取副本,而不是:

deck_of_cards = {suit: cards.copy() for suit in suits}

在处理 lists/dictionaries 时,重要的是要记住赋值,例如:

listA = [1,2,3]
listA = listB

请勿复制您的列表。你可以看到如果你改变listA的一个元素,listB会相应地改变:

listA[2] = 4
print(listB)
# This will output [1,2,4]

因此在分配列表时,您将需要复制。有多种方法,但我建议的两种方法是:

listA = listB[:]

listA = listB.copy()

第一种方法适用于较旧的 python,而据我所知,第二种方法需要 python3+