纸牌排序

Posted 五等码农

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了纸牌排序相关的知识,希望对你有一定的参考价值。

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

以上是关于纸牌排序的主要内容,如果未能解决你的问题,请参考以下文章

纸牌排序

纸牌排序

手写冒泡排序

Win7系统怎样调整D盘中文件夹的顺序

02. 最简单的排序算法——选择排序

插入排序算法