我想从值最接近'haz'
的字典的键中获取值。'foo'
'barn'
这是我效率低下的解决方案:
data = [{'foo': 'bar', 'haz': 'bzr'}, {'foo': 'zoo', 'haz':'bnr'}]
from difflib import get_close_matches
closest = get_close_matches('barn', [elem['foo'] for elem in data])
print 'result =', [elem['haz'] for elem in data if elem['foo']==closest[0]][0]
什么是更好的解决方案?