获取Python的字典值
我有一个Python词典列表,我想检查是否存在特定术语的词典条目。它使用语法工作获取Python的字典值,python,python-2.7,Python,Python 2.7,我有一个Python词典列表,我想检查是否存在特定术语的词典条目。它使用语法工作 if any(d['acronym'] == 'lol' for d in loaded_data): print "found" 但是我还想得到存储在这个键上的值,我的意思是d['acronym']['means']。我的问题是,当我试图打印出来时,Python并不知道d。有什么建议吗?也许我怎样才能在不再次遍历所有列表的情况下获得事件的索引?谢谢 如果您想使用该项目,而不仅仅是检查它是否存在:
if any(d['acronym'] == 'lol' for d in loaded_data):
print "found"
但是我还想得到存储在这个键上的值,我的意思是d['acronym']['means']。我的问题是,当我试图打印出来时,Python并不知道d。有什么建议吗?也许我怎样才能在不再次遍历所有列表的情况下获得事件的索引?谢谢 如果您想使用该项目,而不仅仅是检查它是否存在:
for d in loaded_data:
if d['acronym'] == 'lol':
print("found")
# use d
break # skip the rest of loaded_data
如果您想使用该项目,而不仅仅是检查它是否存在:
for d in loaded_data:
if d['acronym'] == 'lol':
print("found")
# use d
break # skip the rest of loaded_data
any()
只返回一个布尔值,所以不能使用它。所以只要写一个循环:
for d in loaded_data:
if d['acronym'] == 'lol':
print "found"
meaning = d['meaning']
break
else:
# The else: of a for runs only if the loop finished without break
print "not found"
meaning = None
编辑:或将其更改为更通用的函数:
def first(iterable, condition):
# Return first element of iterable for which condition is True
for element in iterable:
if condition(element):
return element
return None
found_d = first(loaded_data, lambda d: d['acronym'] == 'lol')
if found_d:
print "found"
# Use found_d
filter(lambda d: d['acronym'] == 'lol', loaded_data)
any()
只返回一个布尔值,所以不能使用它。所以只要写一个循环:
for d in loaded_data:
if d['acronym'] == 'lol':
print "found"
meaning = d['meaning']
break
else:
# The else: of a for runs only if the loop finished without break
print "not found"
meaning = None
编辑:或将其更改为更通用的函数:
def first(iterable, condition):
# Return first element of iterable for which condition is True
for element in iterable:
if condition(element):
return element
return None
found_d = first(loaded_data, lambda d: d['acronym'] == 'lol')
if found_d:
print "found"
# Use found_d
filter(lambda d: d['acronym'] == 'lol', loaded_data)
您只需使用
filter
功能即可:
def first(iterable, condition):
# Return first element of iterable for which condition is True
for element in iterable:
if condition(element):
return element
return None
found_d = first(loaded_data, lambda d: d['acronym'] == 'lol')
if found_d:
print "found"
# Use found_d
filter(lambda d: d['acronym'] == 'lol', loaded_data)
这将返回包含首字母缩写==lol
的词典列表:
l = filter(lambda d: d['acronym'] == 'lol', loaded_data)
if l:
print "found"
print l[0]
甚至不需要使用
任何
函数。您可以使用过滤器
函数:
def first(iterable, condition):
# Return first element of iterable for which condition is True
for element in iterable:
if condition(element):
return element
return None
found_d = first(loaded_data, lambda d: d['acronym'] == 'lol')
if found_d:
print "found"
# Use found_d
filter(lambda d: d['acronym'] == 'lol', loaded_data)
这将返回包含首字母缩写==lol
的词典列表:
l = filter(lambda d: d['acronym'] == 'lol', loaded_data)
if l:
print "found"
print l[0]
甚至不需要使用任何函数。如果您知道最多有一个匹配项(或者,您只关心第一个匹配项),则可以使用下一个:
>>> loaded_data = [{"acronym": "AUP", "meaning": "Always Use Python"}, {"acronym": "GNDN", "meaning": "Goes Nowhere, Does Nothing"}]
>>> next(d for d in loaded_data if d['acronym'] == 'AUP')
{'acronym': 'AUP', 'meaning': 'Always Use Python'}
然后,根据您希望将异常或None
作为未找到的值:
>>> next(d for d in loaded_data if d['acronym'] == 'AZZ')
Traceback (most recent call last):
File "<ipython-input-18-27ec09ac3228>", line 1, in <module>
next(d for d in loaded_data if d['acronym'] == 'AZZ')
StopIteration
>>> next((d for d in loaded_data if d['acronym'] == 'AZZ'), None)
>>>
如果您知道最多有一个匹配项(或者,您只关心第一个匹配项),则可以使用
next
:
>>> loaded_data = [{"acronym": "AUP", "meaning": "Always Use Python"}, {"acronym": "GNDN", "meaning": "Goes Nowhere, Does Nothing"}]
>>> next(d for d in loaded_data if d['acronym'] == 'AUP')
{'acronym': 'AUP', 'meaning': 'Always Use Python'}
然后,根据您希望将异常或None
作为未找到的值:
>>> next(d for d in loaded_data if d['acronym'] == 'AZZ')
Traceback (most recent call last):
File "<ipython-input-18-27ec09ac3228>", line 1, in <module>
next(d for d in loaded_data if d['acronym'] == 'AZZ')
StopIteration
>>> next((d for d in loaded_data if d['acronym'] == 'AZZ'), None)
>>>
为您提供条件适用的第一个dict,如果没有此类dict,则为None
给出条件适用的第一个dict,或者如果没有这样的dict,则为
None
。谢谢,我只是希望有一种避免这种情况的方法!:-)无论怎样,any
检查都会在幕后进行,因此它的效率不会降低!谢谢,我只是希望有办法避免这些东西无论怎样,any
检查都会在幕后进行,因此它的效率不会降低!太好了,这正是我想要的!!非常感谢!:)@Crista23我很高兴,我更新了这个问题,没有必要使用any
:),但是即使在找到第一个匹配的词典之后,它仍然会循环,这可能很重要,也可能无关紧要。这是真的。OP没有具体说明这一点。DSM answers用iTertor涵盖了这一部分。太好了,这正是我想要的!!非常感谢!:)@Crista23我很高兴,我更新了这个问题,没有必要使用any
:),但是即使在找到第一个匹配的词典之后,它仍然会循环,这可能很重要,也可能无关紧要。这是真的。OP没有具体说明这一点。DSM answers使用iTertor涵盖了这一部分。+1这是一种不迭代整个列表的非常好的方法。+1这是一种不迭代整个列表的非常好的方法。