Sorted()函数没有做我想要的

时间:2016-12-02 03:38:28

标签: python sorting poker

我正在创建一个排名扑克手的程序。我希望在排名之前按照牌的数量值对每一手进行排序,以使其更容易但是在让排序工作时遇到问题。它对self.deck中的每个字母进行排序,而不是单个字符串。

from random import *
class Carddeck:

    def __init__(self, deck=[], hand=[]):
        self.deck=deck
        self.hand=hand
        numbers=['01','02','03','04','05','06','07','08','09','10','11','12']
        suits=['heart','spade','diamond','club']
        for each in numbers:
            for every in suits:
                deck.append(str(Card(each,every)))

    def __repr__(self):

        return str(self.deck)

    def shuffle(self):
        shuffle(self.deck)

        return self.deck

    def dealcards(self,n):
        if len(str(self.deck))>n:
            self.hand=str(self.deck[0:n])
            self.deck = self.deck[n:]
        elif len(self.deck)==n:
            self.hand=str(self.deck[0:n])
            deck=[]
            self.deck=deck
            numbers=['01','02','03','04','05','06','07','08','09','10','11','12']
            suits=['heart','spade','diamond','club']
            for each in numbers:
                for every in suits:
                    self.deck.append(Card(each,every))
        else:
            x=len(self.deck)
            self.hand=self.deck
            self.deck=[]

            numbers=[1,2,3,4,5,6,7,8,9,10,11,12]
            suits=['heart','spade','diamond','club']
            for each in numbers:
                for every in suits:
                    self.deck.append(Card(each,every))
            y=n-x
            self.hand+=str(self.deck[0:y])
            self.deck=self.deck[y-1-3:]


class Pokerhand(Carddeck):
    def newHand(self,hand):
        self.dealcards(5)

    def __repr__(self):
        return str(self.hand)



    def rank(self):
        self.hand=sorted(self.hand)
        return self.hand


>>> x=Carddeck()
>>> x.shuffle()
['01 diamond', '05 heart', '03 club', '05 spade', '09 club', '02 diamond', '03 diamond', '08 diamond', '01 heart', '07 diamond', '11 diamond', '05 club', '02 club', '07 club', '10 club', '01 club', '04 club', '04 heart', '12 club', '11 club', '03 heart', '02 heart', '09 diamond', '08 heart', '06 diamond', '12 spade', '04 diamond', '07 heart', '10 diamond', '12 heart', '09 heart', '08 spade', '07 spade', '03 spade', '02 spade', '08 club', '10 heart', '01 spade', '12 diamond', '11 heart', '06 heart', '06 club', '10 spade', '09 spade', '04 spade', '05 diamond', '06 spade', '11 spade']

>>> y=Pokerhand()
>>> y.newHand(x)
>>> y
['01 diamond', '05 heart', '03 club', '05 spade', '09 club']
>>> y.rank()
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", ',', ',', ',', ',', '0', '0', '0', '0', '0', '1', '3', '5', '5', '9', '[', ']', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd', 'd', 'e', 'e', 'h', 'i', 'l', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'u']

2 个答案:

答案 0 :(得分:1)

sorted对字符串列表进行排序。如果将字符串传递给sorted,它将转换为单字符字符串列表,然后才进行排序。程序中的hand实际上是一个字符串,而不是字符串列表:self.hand=str(self.deck[0:n])

答案 1 :(得分:0)

PokerHand课程中,你的dealcards方法有:

if len(str(self.deck))>n:
    self.hand=str(self.deck[0:n])
    self.deck = self.deck[n:]

所以你最终设置了列表的字符串表示。它对字符串表示进行排序,然后得到您看到的结果,按字典顺序对各个字符进行排序。将hand属性设为字符串是没有意义的。