如何在python中将当前字典嵌套到另一个字典中?

时间:2017-02-17 00:18:23

标签: python nested defaultdict trigram

我有一个默认的dict,它有3层嵌入,稍后将用于trigram。

counts = defaultdict(lambda:defaultdict(lambda:defaultdict(lambda:0)))

然后,我有一个for循环遍历文档并创建每个字母的计数(以及双重和三重奏)

counts[letter1][letter2][letter3] = counts[letter1][letter2][letter3] + 1

我想添加另一个图层,以便我可以指定该字母是辅音还是元音。

我希望能够通过Consonant vs. Vowel而不是字母表中的每一个字母来运行我的二元组和三元组,但我不知道如何做到这一点。

2 个答案:

答案 0 :(得分:0)

我不确定你想要做什么,但我认为嵌套的dict方法并不像拥有一个扁平的dict一样干净,你可以通过组合的字符串来键入(即d['ab']而不是d['a']['b'])。我还输入代码来检查bigram / trigram是否仅由元音/辅音或混合物组成。

CODE:

from collections import defaultdict


def all_ngrams(text,n):
    ngrams = [text[ind:ind+n] for ind in range(len(text)-(n-1))]
    ngrams = [ngram for ngram in ngrams if ' ' not in ngram]
    return ngrams


counts = defaultdict(int)
text = 'hi hello hi this is hii hello'
vowels = 'aeiouyAEIOUY'
consonants = 'bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ'

for n in [2,3]:
    for ngram in all_ngrams(text,n):
        if all([let in vowels for let in ngram]):
            print(ngram+' is all vowels')

        elif all([let in consonants for let in ngram]):
            print(ngram+' is all consonants')

        else:
            print(ngram+' is a mixture of vowels/consonants')

        counts[ngram] += 1

print(counts)

输出:

hi is a mixture of vowels/consonants
he is a mixture of vowels/consonants
el is a mixture of vowels/consonants
ll is all consonants
lo is a mixture of vowels/consonants
hi is a mixture of vowels/consonants
th is all consonants
hi is a mixture of vowels/consonants
is is a mixture of vowels/consonants
is is a mixture of vowels/consonants
hi is a mixture of vowels/consonants
ii is all vowels
he is a mixture of vowels/consonants
el is a mixture of vowels/consonants
ll is all consonants
lo is a mixture of vowels/consonants
hel is a mixture of vowels/consonants
ell is a mixture of vowels/consonants
llo is a mixture of vowels/consonants
thi is a mixture of vowels/consonants
his is a mixture of vowels/consonants
hii is a mixture of vowels/consonants
hel is a mixture of vowels/consonants
ell is a mixture of vowels/consonants
llo is a mixture of vowels/consonants
defaultdict(<type 'int'>, {'el': 2, 'his': 1, 'thi': 1, 'ell': 2, 'lo': 2, 'll': 2, 'ii': 1, 'hi': 4, 'llo': 2, 'th': 1, 'hel': 2, 'hii': 1, 'is': 2, 'he': 2})

答案 1 :(得分:0)

假设您需要保留元音和辅音序列的计数,您可以简单地保留不同的地图。

如果你有一个函数is_vowel(letter),如果True是元音,则返回letter; False如果它是一个辅音,你可以这样做。

vc_counts[is_vowel(letter1)][is_vowel(letter2)][is_vowel(letter3)] = \
vc_counts[is_vowel(letter1)][is_vowel(letter2)][is_vowel(letter3)] + 1