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

2024-03-29 12:07:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个python方法,该方法查看稀有性,对不同类型进行计数,然后返回最小的计数,但我不断得到:“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


Tags: thenameselfgetreturndefcountercard
1条回答
网友
1楼 · 发布于 2024-03-29 12:07:28

那个

for eachCard in self.cardlist 

返回MagicCard实例。在每张卡片上调用get\u rarity()、get\u name()和get\u 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)

相关问题 更多 >