【问题标题】:pop() removing value from multiple lists in dictionarypop() 从字典中的多个列表中删除值
【发布时间】:2021-09-28 15:33:41
【问题描述】:

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']})

如果您可以看到我正在删除每个列表中的“卡片”变量,而不是在选定的“随机套装”列表中删除它。

我已经在这几个小时了。 回报暂时只是为了测试。

我也试过了:

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”。

【问题讨论】:

    标签: python list dictionary dictionary-comprehension


    【解决方案1】:

    问题是每个花色都获得相同的卡片列表 - 相同的实例 - 然后您正在修改(排序和删除)。您可以要求一份副本,而不是:

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

    【讨论】:

    • 那么每个键的值实际上都指向内存中的同一个点?当我删除一个元素时,我是从那个点删除它吗?这就是它的要点吗?
    • 是的,差不多就是这样。 Ned Batchelder 的这次演讲有更好的解释(和图片!),如果你想了解更多:youtube.com/watch?v=_AEJHKGk9ns
    【解决方案2】:

    在处理列表/字典时,请务必记住以下分配:

    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+

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多