标签:dex value port int values key range split() sorted
import collections
Card = collections.namedtuple(‘Card‘,[‘rank‘,‘suit‘])
class FrenchDeck:
ranks = [str(n) for n in range(2,11)] + list(‘JQKA‘)
suits = ‘spades diamonds clubs hearts‘.split()
def __init__(self):
self._cards = [Card(rank,suit) for suit in self.suits for rank in self.ranks]
def __len__(self):
return len(self._cards)
def __getitem__(self, postion):
return self._cards[postion]
suit_values = dict(spades=3,hearts = 2,diamonds = 1,clubs = 0)
def spades_high(card):
rank_value = FrenchDeck.ranks.index(card.rank)
return rank_value*len(suit_values) +suit_values[card.suit]
deck = FrenchDeck()
for card in sorted(deck,key=spades_high):
print(card)
#效果
Card(rank=‘2‘, suit=‘clubs‘)
Card(rank=‘2‘, suit=‘diamonds‘)
Card(rank=‘2‘, suit=‘hearts‘)
Card(rank=‘2‘, suit=‘spades‘)
Card(rank=‘3‘, suit=‘clubs‘)
Card(rank=‘3‘, suit=‘diamonds‘)
Card(rank=‘3‘, suit=‘hearts‘)
Card(rank=‘3‘, suit=‘spades‘)
Card(rank=‘4‘, suit=‘clubs‘)
Card(rank=‘4‘, suit=‘diamonds‘)
Card(rank=‘4‘, suit=‘hearts‘)
Card(rank=‘4‘, suit=‘spades‘)
Card(rank=‘5‘, suit=‘clubs‘)
Card(rank=‘5‘, suit=‘diamonds‘)
Card(rank=‘5‘, suit=‘hearts‘)
Card(rank=‘5‘, suit=‘spades‘)
Card(rank=‘6‘, suit=‘clubs‘)
Card(rank=‘6‘, suit=‘diamonds‘)
Card(rank=‘6‘, suit=‘hearts‘)
Card(rank=‘6‘, suit=‘spades‘)
Card(rank=‘7‘, suit=‘clubs‘)
Card(rank=‘7‘, suit=‘diamonds‘)
Card(rank=‘7‘, suit=‘hearts‘)
Card(rank=‘7‘, suit=‘spades‘)
Card(rank=‘8‘, suit=‘clubs‘)
Card(rank=‘8‘, suit=‘diamonds‘)
Card(rank=‘8‘, suit=‘hearts‘)
Card(rank=‘8‘, suit=‘spades‘)
Card(rank=‘9‘, suit=‘clubs‘)
Card(rank=‘9‘, suit=‘diamonds‘)
Card(rank=‘9‘, suit=‘hearts‘)
Card(rank=‘9‘, suit=‘spades‘)
Card(rank=‘10‘, suit=‘clubs‘)
Card(rank=‘10‘, suit=‘diamonds‘)
Card(rank=‘10‘, suit=‘hearts‘)
Card(rank=‘10‘, suit=‘spades‘)
Card(rank=‘J‘, suit=‘clubs‘)
Card(rank=‘J‘, suit=‘diamonds‘)
Card(rank=‘J‘, suit=‘hearts‘)
Card(rank=‘J‘, suit=‘spades‘)
Card(rank=‘Q‘, suit=‘clubs‘)
Card(rank=‘Q‘, suit=‘diamonds‘)
Card(rank=‘Q‘, suit=‘hearts‘)
Card(rank=‘Q‘, suit=‘spades‘)
Card(rank=‘K‘, suit=‘clubs‘)
Card(rank=‘K‘, suit=‘diamonds‘)
Card(rank=‘K‘, suit=‘hearts‘)
Card(rank=‘K‘, suit=‘spades‘)
Card(rank=‘A‘, suit=‘clubs‘)
Card(rank=‘A‘, suit=‘diamonds‘)
Card(rank=‘A‘, suit=‘hearts‘)
Card(rank=‘A‘, suit=‘spades‘)
进程已结束,退出代码0
标签:dex value port int values key range split() sorted
原文地址:https://www.cnblogs.com/wudeng/p/9025866.html