1

我想从值最接近'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]

什么是更好的解决方案?

4

1 回答 1

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']

我会找到一个更快的编辑距离实现,因为调用SequenceMatcherget_close_matches在内部进行)不是一个理想的解决方案。

于 2012-12-10T04:27:45.283 回答