找到对象中不同计数的最小值

时间:2017-11-23 15:05:53

标签: python object instances

我试着编写一个python方法来查看Rarity并计算不同的类型,然后返回最小的数量,但我一直得到:' MagicCard'对象不可订阅。我还想检查哪种颜色与最不罕见的卡片相关联。任何帮助将不胜感激:

以下是可以从中获取Json以进行测试的地方:http://mtgjson.com/json/DTK.json

卡片对象:

class MagicCard(object):
    def __init__ (self, jsonCard):
        self.name=jsonCard["name"]

        if jsonCard.get("colors",""):
            self.colors=jsonCard["colors"]
        else:
            self.colors=None

        if jsonCard["rarity"]:
            self.rarity=jsonCard["rarity"]
        else:
            self.rarity=None

    def get_name(self):
        """Return the name of the card"""
        return self.name

    def get_colors(self):
        """Return the colors of the card"""
        return self.colors

    def get_rarity(self):
        """Return the rarity of the card"""
        return self.rarity   

卡片对象:

class MagicCardSet(object):
    def __init__(self, DeckDict):
        self.cardlist = [MagicCard(eachCard) for eachCard in DeckDict["cards"]]

    def get_card_list(self):
        Card_name_list=[]
        for newCard in self.cardlist:
            Card_name_list.append(newCard.get_name())
        return(Card_name_list)

    def get_card_color(self):
        color_list=[]
        for newCard in self.cardlist:
            color_list.append(newCard.get_color())
        return(color_list)

    def get_card_rarity(self):
        rarity_list=[]
        for newCard in self.cardlist:
            rarity_list.append(newCard.get_rarity())
        return(rarity_list)

    def get_rarest_card(self):
        for eachCard in self.cardlist:
            if eachCard["rarity"]=="Uncommon":
                uncommon_counter = uncommon_counter + 1
            elif eachCard["rarity"]=="Common":
                common_counter=common_counter + 1
            elif eachCard["rarity"]=="Rare":
                rare_counter = rare_counter + 1
            elif eachCard["rarity"]=="Mythic Rare":
                mythic_rare_counter = mythic_rare_counter + 1
        return(mythic_rare_counter)

错误: enter image description here

1 个答案:

答案 0 :(得分:0)

for eachCard in self.cardlist 

返回一个MagicCard实例。在eachCard上调用get_rarity()和get_name()以及get_colors()。

另外,要获得最不常见(最常见)卡片的颜色:

unrare_colors = {}
for eachCard in self.cardlist:
    if eachCard.get_rarity() == "Common":
        for eachColor in eachCard.get_colors():
            unrare_colors[eachColor] = 1

for color in unrare_colors.keys():
    print(color)