卡片组订购

我需要能够在洗牌后对卡座进行分类。我的想法是将列表重新分成两个组件,并检查每个组件是否有序,然后重新组合。卡片组订购

如何从Deck类内部分别访问价值部分和适合部分?

如果你对如何做到这一点有个更好的想法,我也会很感激。

.sort()由于列表中的项是char + int,即('2C','KH'),调用将不起作用。

import random 

class Card:

def __init__(self, suit, order):

self.order = order

self.suit = suit

def fan(self):

print(self.order, "of", self.suit)

class Deck():

def __init__(self):

self.deck = []

for suit in ['Clubs', 'Diamonds', 'Hearts', 'Spades']:

for order in ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']:

self.deck.append(Card(suit, order))

def fan(self):

for c in self.deck:

c.fan()

def shuffle(self):

for suit in ['Clubs', 'Diamonds', 'Hearts', 'Spades']:

for order in ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']:

self.deck.append(Card(suit, order))

random.shuffle(self.deck)

def deal(self):

return self.deck.pop()

def isOrdered(self):

pass

def Order(self):

pass

回答:

“教”的卡对象如何互相比较:

sort()方法要求的卡对象必须能够至少“答案”的问题card1 < card2所以Card类需要一个额外的方法:

def __lt__(self, other): 

"""

Returns self < other

"""

# Following two should better be defined globally (outside of method

# and maybe outside of the Card class

SUIT_LIST = ['Clubs', 'Diamonds', 'Hearts', 'Spades']

ORDER_LIST = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']

if self.suit == other.suit:

return ORDER_LIST.index(self.order) < ORDER_LIST.index(other.order)

else:

return SUIT_LIST.index(self.suit) < SUIT_LIST.index(other.suit)

现在卡对象可以<进行比较和卡的对象列表进行排序。

以上是 卡片组订购 的全部内容, 来源链接: utcz.com/qa/259158.html

回到顶部