擅长:python、mysql、java
<p>没有超短的解决方案。你将不得不遍历整个列表,不过如果你找到你想要的,你可以短路。你知道吗</p>
<p>您可以使用以下函数执行此操作:</p>
<pre><code>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)
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>Portal
</code></pre>
<p>注意,我可能会使用一个<code>tuples</code>列表或一个<code>namedtuples</code>列表,如果这是您为节省内存所做的一切。你知道吗</p>