我有一组对象。对象是同一个类的实例。我需要验证变量reviewed_object_name
是否出现在集合中任何对象的object_name
字段中。
当我有一个简单的集合时,解决方案是显而易见的:
reviewed_object_name = "Hotel Roma"
reviewed_objects_collection = ["Hotel Roma", "Hotel Berlin", "Hotel Paris", "Hotel Madrit"]
reviewed_object_name in reviewed_objects_collection
表达式返回True
当我有一个对象集合
时会出现问题我尝试使用lambda:
some_function (reviewed_objects_collection, key = lambda review_object_info: review_object_info.name)
什么功能可以给我这个信息?
答案 0 :(得分:2)
any(reviewed_object_name == reviewed_object.name for reviewed_object in reviewed_objects_collection)