l = [
{"Abbv": "VIS", "Set": "Visions"},
{"Abbv": "5ED", "Set": "Fifth Edition"},
{"Abbv": "POR", "Set": "Portal"},
{"Abbv": "WTL", "Set": "Weatherlight"}
]
def search_for_abbv(abbv, l):
"""
l: A list of dicts we want to search through.
abbv: The abbreviation we want to find
Returns None if nothing found else the abbreviation.
"""
for item in l:
if abbv == item['Abbv']:
return item['Set']
temp = search_for_abbv('POR', l)
print(temp)
你是说这样的事吗?你知道吗
没有超短的解决方案。你将不得不遍历整个列表,不过如果你找到你想要的,你可以短路。你知道吗
您可以使用以下函数执行此操作:
输出:
注意,我可能会使用一个
tuples
列表或一个namedtuples
列表,如果这是您为节省内存所做的一切。你知道吗我想这是你想要的结果。您只需指定要搜索的成对查找,并尝试在JSON对象列表中找到它。找到它之后,访问这个对象的
'Set'
键。你知道吗相关问题 更多 >
编程相关推荐