问题:如何验证对象集合中对象的从属关系

时间:2011-06-19 14:55:35

标签: python lambda iterator

我有一组对象。对象是同一个类的实例。我需要验证变量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)

什么功能可以给我这个信息?

1 个答案:

答案 0 :(得分:2)

带有genex的

any()

any(reviewed_object_name == reviewed_object.name for reviewed_object in reviewed_objects_collection)