如何用一种或多种不同类型的标签为对象添加标签,以供日后使用?

时间:2019-05-09 23:50:21

标签: python class if-statement

我正在尝试编写一个脚本,该脚本包含一张纸牌列表,将它们变成纸牌对象,然后随机播放并运行测试手,以便您根据手中的手数知道一手牌的可能性甲板上。 (理想情况下,我想让它一次模拟10,000手)。

问题是我不知道如何将这些卡片对象标记为一件或多件事情。例如,红心皇后有很多东西:它是一张脸卡,一张皇后卡,而且是红心套装。

现在,我要获取一张卡片列表以及卡片组中有多少张卡片,并使用一个类将它们转变为卡片对象列表。最终将返回一个带有卡名称和类别的元组列表。然后,我使用一系列的if语句,根据哪些卡片需要什么标签的硬编码列表将卡片分类为某些类别。

问题是我不知道如何一次给他们一个以上的标签。

我也是python的新手,所以我不知道这是否是执行此操作的明智方法。

List = [("queen of hearts", 4), ("king of hearts", 4), ("joker", 3), ("2 of clubs", 4), ("4 of clubs", 2), ("2 of diamonds", 3)]

Type_hearts = {"queen of hearts", "king of hearts", "2 of hearts", "ace of heats"}
Type_face = {"queen of hearts", "queen of diamonds", "king of diamonds", "joker"}
Type_queen = {"queen of hearts", "queen of diamonds"}
Type_joker = {"joker"}

class Card(object):

    def __init__(self, name):
        self.name = name
        #the following if-statement codes card objects into types
        ##in order for an object to fit into these categories, the full and complete name must match exactly
        ###also, right now it looks like these can currently only be one thing at a time :(
        if name in Type_hearts:
            Type = "heart"
        if name in Type_face:
            Type = "face card"
        if name in Type_queen:
            Type = "is queen"
        else:
            Type = None
        self.card_type = Type


    def __repr__(self):
        return f"<{self.name}, {self.card_type}>"
    #now we need to tell the computer how to REPRESENT this class as a string
        #when you implement one of these, just implement both of these to have a textual representation of your class.

    def __str__(self):
        return f"<{self.name}, {self.card_type}>"

class DeckList(object):
    def __init__(self, decklist):
        self.decklist = decklist
        #self.name = name 

        #self.count = count 
        self.deck = []
        #decklist
        for name, count in decklist:
            for i in range(count):
                self.deck.append(Card(name))

我很想为程序找到一种方法,可以将卡片上的对象(例如,红心大王)初始化,并知道它是国王,面部卡片和西服的心。到目前为止,我只是不知道该怎么做,而且我一次只能标记一件事情。

2 个答案:

答案 0 :(得分:0)

您可以分配一个列表,而不是为card_type分配一个值,这样该类就变成了-


    def __init__(self, name):
        self.name = name
        #the following if-statement codes card objects into types
        ##in order for an object to fit into these categories, the full and complete name must match exactly
        ###also, right now it looks like these can currently only be one thing at a time :(
        type_list = []
        if name in Type_hearts:
            type_list.append("heart")
        if name in Type_face:
            type_list.append("face card")
        if name in Type_queen:
            type_list.append("is queen")
        self.card_type = type_list


    def __repr__(self):
        return f"<{self.name}, {self.card_type}>"
    #now we need to tell the computer how to REPRESENT this class as a string
        #when you implement one of these, just implement both of these to have a textual representation of your class.

    def __str__(self):
        return f"<{self.name}, {self.card_type}>"```

答案 1 :(得分:0)

看来您可能想对字符串做太多事情。我们有一个Card类是好的。我们目前正在以human readable格式传递卡的名称,并处理该格式。如果我们重组数据以使其更易于使用怎么办。

我们不只是取一个名字,而是如果我们有更多的价值

class Card:
    def __init__(self, name, suit, value):
        self.name = name
        self.suit = suit 
        self.value = value

我们可以开始在类中添加一些辅助方法以提取所需的数据

    def is_face(self):
        return self.value > 10 # Jack and up is a face card

    def __str__(self):
        return "{} of {}".format(self.name, self.suit)

    def is_queen(self):
        return self.name == "Queen"

    def is_king(self):
        return self.name == "King"

我们可以用

king_of_hearts = Card("King", "Hearts", 13)
queen_of_spades = Card("Queen" ,"Spades", 12)
ten_of_diamonds = Card("Ten", "Diamonds", 10)

print(king_of_hearts) # King of Hearts
print(queen_of_spades) # Queen of Spades
print(ten_of_diamonds) # Ten of Diamonds

我们应该能够根据其数据确定该卡的type。我们不需要在构建时检查它应该具有什么类型。