2011-06-19 183 views
0

我有一個對象集合。對象是同一個類的實例。我需要驗證變量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

時,我有對象的集合

我試圖做到這一點使用拉姆達問題出現:

some_function (reviewed_objects_collection, key = lambda review_object_info: review_object_info.name) 

什麼函數可以給m e此信息?

回答

2

any()與genex。

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