Python: Indexing tuples to create new dictionaries

2024-04-26 03:50:38 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以基于单个字典中唯一的第一元组值创建新的单个字典,并映射相应的键和值?你知道吗

工作说明:

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名称是元组中唯一的第一个元素,键和值相应地映射?你知道吗


Tags: fromimportregetstring字典thatcounter
1条回答
网友
1楼 · 发布于 2024-04-26 03:50:38

创建一个字典,其关键字是第一个单词,其值是第二个单词和频率的字典,例如:

from collections import defaultdict
import re

text = 'this dog, this dog. this, animal this animal animal this animal this dog that animal'
words = re.findall('\w+', TestString)
counts = defaultdict(lambda: defaultdict(int))
for word1, word2 in zip(words, words[1:]):
    counts[word1][word2] += 1

这将给你counts作为:

defaultdict(<function <lambda> at 0x7f0ca8565378>,
            {'animal': defaultdict(<class 'int'>, {'animal': 1, 'this': 3}),
             'dog': defaultdict(<class 'int'>, {'that': 1, 'this': 2}),
             'that': defaultdict(<class 'int'>, {'animal': 1}),
             'this': defaultdict(<class 'int'>, {'animal': 3, 'dog': 3})})

然后类似counts['this']['dog']的东西将返回3。。。等。。。你知道吗

相关问题 更多 >