我对纸牌手的排序功能仅按西装而不是按值排序。 [python]

时间:2019-03-13 17:15:46

标签: python sorting

我知道以前已经有人问过这个问题,但是我正在寻找一种更干净的解决方案。我想按西装和顺序对一手牌进行排序。

以下是我的Deck课程的相关部分:

import card
import random

class Deck:

    def __init__(self):
        suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
        values = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" ,"A"]

        self._cards = [card.Card(suit, value) for suit in suits for value in values]

    def deal_hand(self):
        hand = random.sample(self._cards, 5)
        for card in hand:
            self._cards.remove(card)
        hand.sort(key=lambda x: (x.suit, x.value))
        return hand

这是我的卡类:

class Card:

    def __init__(self, suit, value):
        self.suit = suit
        self.value = value

    def __repr__(self):
        return f"{self.value} of {self.suit}"

但是,当我看玩家的手时,它们仅按衣服排序,而不按值排序。

这是我得到的输出:

John's hand: [10 of Clubs, 2 of Clubs, A of Hearts, 9 of Spades, J of Spades]

Arnold's hand: [K of Clubs, 8 of Diamonds, 7 of Hearts, 9 of Hearts, 7 of Spades]

Alex's hand: [Q of Clubs, 2 of Hearts, 5 of Hearts, 8 of Spades, K of Spades]

Morgan's hand: [5 of Clubs, A of Diamonds, 10 of Hearts, Q of Hearts, 2 of Spades]

其中10个俱乐部排在2个俱乐部之前。另外,x的K将在x的Q之前。

为什么我的lambda只按西装排序?我至少希望其中的10个俱乐部能排在2个俱乐部之后。

有干净的溶液吗?

0 个答案:

没有答案