向字典添加计数器和距离
Add counter and distance to dictionary
你好,我有一个特定的字符串,我正在尝试使用编辑距离计算它的距离,我想查看出现的字符串的计数,然后对其进行排序。
str= "Hello"
我正在比较的名为 xfile 的 txt 文件是:
"hola"
"how are you"
"what is up"
"everything good?"
"hola"
"everything good?"
"what is up?"
"okay"
"not cool"
"not cool"
我想制作一个字典,将所有行与 xfile 进行比较,并给出编辑距离和计数。
现在,我能够得到它的关键和距离,但不是计数。
有人可以建议我吗?
我的代码是:
data= "Hello"
Utterences = {}
for lines in readFile:
dist= editdistance.eval(data,lines)
Utterances[lines]= dist
对于每个话语,您都可以有一个包含距离和计数的字典:
import editdistance
data = 'Hello'
utterances = {}
xlist = [
'hola',
'how are you',
'what is up',
'everything good?',
'hola',
'everything good?',
'what is up?',
'okay',
'not cool',
'not cool',
]
for line in xlist:
if line not in utterances:
utterances[line] = {
'distance': editdistance.eval(data, line),
'count': 1
}
else:
utterances[line]['count'] += 1
然后,如果您需要按距离或计数对话语进行排序,您可以使用 OrderedDict:
from collections import OrderedDict
sorted_by_distance = OrderedDict(sorted(utterances.items(), key=lambda t: t[1]['distance']))
sorted_by_count = OrderedDict(sorted(utterances.items(), key=lambda t: t[1]['count']))
你好,我有一个特定的字符串,我正在尝试使用编辑距离计算它的距离,我想查看出现的字符串的计数,然后对其进行排序。
str= "Hello"
我正在比较的名为 xfile 的 txt 文件是:
"hola"
"how are you"
"what is up"
"everything good?"
"hola"
"everything good?"
"what is up?"
"okay"
"not cool"
"not cool"
我想制作一个字典,将所有行与 xfile 进行比较,并给出编辑距离和计数。 现在,我能够得到它的关键和距离,但不是计数。 有人可以建议我吗?
我的代码是:
data= "Hello"
Utterences = {}
for lines in readFile:
dist= editdistance.eval(data,lines)
Utterances[lines]= dist
对于每个话语,您都可以有一个包含距离和计数的字典:
import editdistance
data = 'Hello'
utterances = {}
xlist = [
'hola',
'how are you',
'what is up',
'everything good?',
'hola',
'everything good?',
'what is up?',
'okay',
'not cool',
'not cool',
]
for line in xlist:
if line not in utterances:
utterances[line] = {
'distance': editdistance.eval(data, line),
'count': 1
}
else:
utterances[line]['count'] += 1
然后,如果您需要按距离或计数对话语进行排序,您可以使用 OrderedDict:
from collections import OrderedDict
sorted_by_distance = OrderedDict(sorted(utterances.items(), key=lambda t: t[1]['distance']))
sorted_by_count = OrderedDict(sorted(utterances.items(), key=lambda t: t[1]['count']))