在Python中存储和比较多个属性

时间:2018-08-04 02:05:03

标签: python class dictionary compare

我正在尝试制作一个幸存者游戏,该游戏将使我能够以部落成员的身份分别比较存储的角色。我可以用一个属性来完成它,但是无法找出将其扩展到多个属性的最佳方法。

示例:

TribeAStr = {'John': 50, 'Tyler': 55, 'Joe': 90, 'Bob': 55}

# check who is the strongest Survivor
$ strongest = max(TribeAStr, key=TribeAStr.get)

#Remove him from TribeA
del TribeAStr[strongest]

#Check how strong the Tribe is as a whole
$ tribeastrength = sum(TribeAStr.values())

我想做的是让每个幸存者拥有自己的属性,然后将其名称用作密钥,然后将其引入上述类似的情况中。像这样:

class Survivor:
    def __init__(self, name, str=None, int=None, cha=None, luk=None):
        self.name = name
        self.str = strength
        self.int = intelligence
        self.cha = charisma
        self.luk = luck

TribeA = {}
John = Survivor{'John', 50, 40, 55, 20}
Bob= Survivor{'Bob', 55, 40, 80, 25}
Joe= Survivor{'Joe', 60, 10, 65, 10}
Tyler= Survivor{'Tyler', 56, 30, 15, 25}
TribeA.update('John', 'Bob', 'Joe', 'Tyler')

# check who is the strongest Survivor
$ strongest = max(Survivor, Survivor.str) for Survivor in TribeA

有什么建议吗?

3 个答案:

答案 0 :(得分:0)

这应该有效:

In [19]: class Survivor:
    ...:     def __init__(self, name, strength=None, intelligence=None, charisma=None, luck=None):
    ...:         self.name = name
    ...:         self.strength = strength
    ...:         self.intelligence = intelligence
    ...:         self.cha = charisma
    ...:         self.luk = luck

In [21]: TribeA = []
    ...: John = Survivor('John', 50, 40, 55, 20)
    ...: Bob= Survivor('Bob', 55, 40, 80, 25)
    ...: Joe= Survivor('Joe', 60, 10, 65, 10)
    ...: Tyler= Survivor('Tyler', 56, 30, 15, 25)
    ...: TribeA.extend([John, Bob, Joe, Tyler])

In [25]: strongest = max(TribeA, key=lambda x: x.strength)

In [26]: strongest.name
Out[26]: 'Joe'

要从TribeA中删除最强的玩家,请使用以下命令:

TribeA = list(filter(lambda x: x != strongest, TribeA))

# or going by name
# TribeA = list(filter(lambda x: x.name != strongest.name, TribeA))

答案 1 :(得分:0)

您的第一个问题是语法错误:

Bob

在Python中,您可以像调用普通函数一样在构造函数中加上括号:

John = Survivor{'John', 50, 40, 55, 20}

您的第二个问题是,仅凭键列表就不能John = Survivor('John', 50, 40, 55, 20) 字典;您需要使用字典(或键值对列表)进行更新。否则它将如何知道您想要什么值?所以:

update

但是,实际上没有理由仅TribeA.update({'John': John, 'Bob': Bob, 'Joe': Joe, 'Tyler': Tyler}) 创建一个空字典;一次性创建它:

update

如果每个名字重复写两次似乎很重复,则可以通过dict理解来解决:

TribeA = {'John': John, 'Bob': Bob, 'Joe': Joe, 'Tyler': Tyler}

或者,也许您不想一开始就创建这些变量;您可以直接将对象直接存储在字典中:

TribeA = {survivor.name: survivor for survivor in (John, Bob, Joe, Tyler)}

第三个问题是您理解错误的语法:

TribeA = {}
TribeA['John'] = Survivor('John', 50, 40, 55, 20)
# etc.

您必须将整个理解放在传递给strongest = max(Survivor, Survivor.str) for Survivor in TribeA 的括号内。另外,您希望按强度对它们进行排序,因此您希望强度在配对中排在第一位,而不是发送。最后,如果您的部落是一个字典,那么您就不能仅遍历该字典,而只是给您钥匙。您需要遍历值:

max

但是请注意,这不仅会给您带来最强的幸存者,而且会给您带来两个价值对,即最强的幸存者的力量和幸存者。您可以扔掉您不想要的那个:

strongest = max((survivor.str, survivor) for survivor in TribeA.values())

……,但最好使用键功能,如Sorting HOWTO中所述:

_, strongest = max((survivor.str, survivor) for survivor in TribeA.values())

答案 2 :(得分:-1)

您可以定义一种方法,用于确定Survivor(可能是所有属性的sum)的总体评级,并将其用作key的{​​{1}}最强的max

Survivor

这将输出:

class Survivor:
    def __init__(self, name, str=None, int=None, cha=None, luk=None):
        self.name = name
        self.str = str
        self.int = int
        self.cha = cha
        self.luk = luk

    def overall(self):
        return sum(getattr(self, attr) for attr in ('str', 'int', 'cha', 'luk'))

John = Survivor('John', 50, 40, 55, 20)
Bob= Survivor('Bob', 55, 40, 80, 25)
Joe= Survivor('Joe', 60, 10, 65, 10)
Tyler= Survivor('Tyler', 56, 30, 15, 25)
TribeA = [John, Bob, Joe, Tyler]
strongest = max(TribeA, key=Survivor.overall)
print(strongest.name)