Python 如何通过引用类实例值来迭代列表?
我正在用python制作一个冒险游戏,我有一个剑类-我有一个函数,其目的是找到列表中最强大的剑(我稍后将修改此函数,使其成为一个玩家清单,但与此无关)。我一直得到这样一个错误:“int类型是不可迭代的”,这对我来说很奇怪,因为当它只是一个数字,而不是对类实例中的值的引用时,它似乎对其他人有效。有人能帮我吗?谢谢Python 如何通过引用类实例值来迭代列表?,python,list,max,Python,List,Max,我正在用python制作一个冒险游戏,我有一个剑类-我有一个函数,其目的是找到列表中最强大的剑(我稍后将修改此函数,使其成为一个玩家清单,但与此无关)。我一直得到这样一个错误:“int类型是不可迭代的”,这对我来说很奇怪,因为当它只是一个数字,而不是对类实例中的值的引用时,它似乎对其他人有效。有人能帮我吗?谢谢 class Sword: def __init__(self, name=None, strength=None, description=None): self
class Sword:
def __init__(self, name=None, strength=None, description=None):
self.name = name
self.strength = strength
self.description = description
rusty_sword = Sword(
name="rusty sword",
strength=5,
description="This is a rusty old sword that you found on the ground.",
)
gold_sword = Sword(
name="gold sword",
strength=15,
description="This is a fine golden sword, with a crown engraved on the handle.",
)
diamond_sword = Sword(
name="diamond sword",
strength=45,
description="This 100% pure diamond sword is of the finest quality. It reflects a prism of light when you turn it back and forth.",
)
plasma_sword = Sword(
name="plasma sword",
strength=135,
description="This plasma sword can slay any opponent. With this, you are unstoppable.",
)
def mostpowerfulsword():
all_swords = (rusty_sword, gold_sword, diamond_sword, plasma_sword)
for sword in all_swords:
swordstrength = sword.strength
print(max(swordstrength))
您正在调用
swarsestrength
上的max
函数,该函数是int
。在循环的每次迭代中,您都会覆盖browstrength
值。我怀疑您想要构建一个列表并将其传递给max
函数
因此,您应该将mostpowerfulsword
函数更改为如下所示:
def mostpowerfulsword():
all_swords = (rusty_sword, gold_sword, diamond_sword, plasma_sword)
swordstrengths = []
for sword in all_swords:
swordstrengths.append(sword.strength)
print(max(swordstrengths))
您可以为
max
指定key
属性,并用lambda
-表达式指示要查找最大值的属性:
max(所有剑,键=lambda x:x.strength)。名称
将提供:
Out[15]: 'plasma sword'
然后,您的函数可能如下所示:
def获得最强大的剑(*剑):
返回最大值(剑,钥匙=λx:x力量)
你会这样称呼它:
get_most_powerful_sword(rusty_sword, gold_sword, diamond_sword, plasma_sword)
注意:如果
strength
是您可以用来比较swarm
类中对象的唯一属性,我建议您使用我强烈建议您覆盖类中的比较行为:
class Sword:
def __init__(self, name=None, strength=None, description=None):
self.name = name
self.strength = strength
self.description = description
def __lt__(self, other):
return self.strength < other.strength
def __gt__(self, other):
return self.strength > other.strength
def __eq__(self, value):
return self.strength == other.strength
rusty_sword = Sword(
name="rusty sword",
strength=5,
description="This is a rusty old sword that you found on the ground.",
)
gold_sword = Sword(
name="gold sword",
strength=15,
description="This is a fine golden sword, with a crown engraved on the handle.",
)
diamond_sword = Sword(
name="diamond sword",
strength=45,
description="This 100% pure diamond sword is of the finest quality. It reflects a prism of light when you turn it back and forth.",
)
plasma_sword = Sword(
name="plasma sword",
strength=135,
description="This plasma sword can slay any opponent. With this, you are unstoppable.",
)
或者
max([所有剑中剑的力量])
@SimonO'Hanlon无需实际列出一个清单。使用生成器表达式:)max(所有剑中剑的剑力)
@ReutSharabani Nice!甚至更好。@ReutSharabani您的版本将给出属性strength
的最大值,而不是实际的Swarm
对象,即您的版本将输出135
,并提供上述示例。如果你想找到与之相关的剑,那么它不是很有用……因此,你的mostpowerfulsword
是135
。看起来更像一个数字而不是一把剑,不是吗?!仅仅是一个风格说明,mostpowerfulsword
函数应该完全按照python的风格约定命名,即get\u most\u power\u swool
,并且它应该接受一个剑列表作为参数,而不是将列表(从技术上讲,您硬编码了一个元组)硬编码在其中。
swords = [rusty_sword, gold_sword, diamond_sword, plasma_sword]
print(f"The strongest sword of them all is: {max(swords).name}")