我想从'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]
什么是更好的解决方案?
答案 0 :(得分:1)
你可以这样做:
from difflib import SequenceMatcher
from functools import partial
from operator import itemgetter
data = [
{'foo': 'bar', 'haz': 'bzr'},
{'foo': 'zoo', 'haz': 'bnr'},
{'foo': 'potato', 'haz': 'test'}
]
matcher = partial(SequenceMatcher, 'barn') # I'd find a better distance function
pairs = enumerate(map(itemgetter('foo'), data)) # Constructs index, value pairs
index, value = sorted(pairs, key=lambda x: matcher(x[1]).ratio())[0]
print data[index]['haz']
我发现更快的编辑距离实现,因为调用SequenceMatcher
(get_close_matches
内部执行)不是理想的解决方案。