只是想知道是否有一种更优雅的方法根据对象是否包含特定的值从列表中的特定对象中检索值,或者我是否需要编写一些东西来遍历列表并查看每个对象。例如:
class C(object):
def __init__(self, url, value):
self.url=url
self.value=value
obj1 = C("http://1", 1)
obj2 = C("http://2", 2)
mylist = [obj1, obj2]
# I want to search mylist and retrieve the "value" element if there is
# an object with a "url" value of "http://2"...basically retrieve the
# value 2 if an element exists in the list with a url value of "http://2"
当然,如果我知道它存在于列表的第一个元素中,我可以通过以下方式检索它:
mylist[1].value
但是,在我的例子中,我不知道该对象是否存在于列表中,也不知道它存在于列表中的哪个位置。