是否可以基于单个字典中唯一的第一元组值创建新的单个字典,并映射相应的键和值?你知道吗
工作说明:
import re
from collections import Counter
TestString = 'this dog, this dog. this, animal this animal animal this animal this dog that animal'
def get_dict(string):
# Create list of individual words without punctuation
x = re.findall(r"[\w']+", string)
# Get sum of individual two-word shingles from list
x = (Counter(zip(x,x[1:])))
print x
get_dict(TestString)
返回以下dict:
Counter({('this', 'dog'): 3, ('this', 'animal'): 3, ('animal', 'this'): 3, ('dog', 'this'): 2, ('animal', 'animal'): 1, ('dog', 'that'): 1, ('that', 'animal'): 1})
由此可以创建这些单独的dict:
this = {'dog': 3, 'animal': 3}
animal = {'this': 3, 'animal': 1}
dog = {'this': 2, 'that': 1}
that = {'animal': 1}
其中新的dict名称是元组中唯一的第一个元素,键和值相应地映射?你知道吗
创建一个字典,其关键字是第一个单词,其值是第二个单词和频率的字典,例如:
这将给你
counts
作为:然后类似
counts['this']['dog']
的东西将返回3
。。。等。。。你知道吗相关问题 更多 >
编程相关推荐