所以我正在努力寻找一种 pythonic(或者我应该说是功能性的?)方法来做到这一点。我有一个对象列表,我想对对象属性执行 bool 比较。如果每个元素的计算结果为真,我希望结果为真;如果一个元素的计算结果不为真,我希望结果为假。希望下面的代码显示了我正在尝试做的事情。我知道我可以用蛮力的方式做到这一点,但我不想...大声笑
class obj
def __init__(self, x):
self.x = x
... somewhere else in the code
objects_list = list of class obj
bool_result = true if all obj.x in list == some value
谢谢!
请您参考如下方法:
你或许可以使用 all :
Return True if all elements of the iterable are true (or if the iterable is empty).
print all(item.x == some_value for item in my_list)
