我正在用 python 进行一些基本的卡/牌组操作。下面你可以看到我的 Card 类和 Deck 类。假设我知道有些牌已经死了,并且想将它们从牌组中删除。
import itertools
SUIT_LIST = ("h", "s", "d", "c")
NUMERAL_LIST = ("2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A")
class Card:
def __init__(self, numeral, suit):
self.numeral = numeral
self.suit = suit
self.card = self.numeral, self.suit
def __repr__(self):
return self.numeral + self.suit
class Deck(set):
def __init__(self):
for numeral, suit in itertools.product(NUMERAL_LIST, SUIT_LIST):
self.add(Card(numeral, suit))
deck = Deck()
dead_card = Card('J','s')
deck.remove(dead_card)
引发以下错误:
Traceback (most recent call last):
File "<ipython-input-93-06af62ea1273>", line 23, in <module>
deck.remove(dead_card)
KeyError: Js
从我的套牌中移除死牌的正确方法是什么?为什么我的方法不起作用?