python - 检查某个属性是否等于任何对象列表中的特定字符串

标签 python

每个对象都有一个属性 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/

相关文章:

python - 重新启动在 Apache + mod_python 上运行的 Django 应用程序

Python 3 : Convert CSV

python - 从字符串列表列表转换为 Python 中的整数列表列表

python - 如何找到与数字列表对应的名称?

python - Django : SyntaxError 'unexpected EOF' while saving a Form

python - 求一个字符串中能被数字k整除的子串的个数

python - 不理解 Pandas.Series.from_csv() 的输出

python - 如何向下滚动网页直到单击特定按钮?

python - python 中的正则表达式使用 OR 表示单引号或双引号

python - 在node js服务器中通过http请求接收二进制数据