每个对象都有一个属性 name
。我想检查特定字符串是否包含在这些对象的 name
中。属性。
[<bouncy.ball object at 0x7fdf8649ef10>, <bouncy.ball object at 0x7fdf863616d0>, < bouncy.ball object at 0x7fdf863616d0>]
最有效的方法是什么?
最佳答案
其他答案使用您已经使用的轨道(“任何”)为您提供答案。如果您想知道在哪个球对象中找到特定名称,您可以使用:
bouncyballs = [<bouncy.ball object at 0x7fdf8649ef10>, <bouncy.ball object at 0x7fdf863616d0>, < bouncy.ball object at 0x7fdf863616d0>]
C = 'string'
nameFound = False
for ball in bouncyballs:
if ball.name == C:
# Do what you like
print('%s is in %s!' % (C, ball['name']))
nameFound = True
print(nameFound)
关于python - 检查某个属性是否等于任何对象列表中的特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34849764/