给定dict1 key2时找到dict1 key1的值?

时间:2012-12-10 04:06:13

标签: python search dictionary key-value lookup-tables

我想从'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]

什么是更好的解决方案?

1 个答案:

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

我发现更快的编辑距离实现,因为调用SequenceMatcherget_close_matches内部执行)不是理想的解决方案。