如何将当前字典嵌套到 python 中的另一个字典中?
How do I nest a current dictionary into another one in python?
我有一个默认的字典,它有 3 层嵌入,稍后将用于三元组。
counts = defaultdict(lambda:defaultdict(lambda:defaultdict(lambda:0)))
然后,我有一个 for 循环遍历文档并创建每个字母的计数(以及双计数和三计数)
counts[letter1][letter2][letter3] = counts[letter1][letter2][letter3] + 1
我想再添加一层,这样我就可以指定字母是辅音字母还是元音字母。
我希望能够 运行 我的二元组和三元组比较辅音与元音而不是字母表中的每个字母,但我不知道该怎么做。
我不确定你到底想做什么,但我认为嵌套字典方法不如使用组合字符串键入的平面字典那样干净(即 d['ab']
d['a']['b']
)。我还输入代码来检查 bigram/trigram 是否仅由 vowels/consonants 或混合物组成。
代码:
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})
假设您需要对元音和辅音的顺序进行计数,您可以简单地保留一个不同的映射。
如果你有一个函数 is_vowel(letter)
returns 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
我有一个默认的字典,它有 3 层嵌入,稍后将用于三元组。
counts = defaultdict(lambda:defaultdict(lambda:defaultdict(lambda:0)))
然后,我有一个 for 循环遍历文档并创建每个字母的计数(以及双计数和三计数)
counts[letter1][letter2][letter3] = counts[letter1][letter2][letter3] + 1
我想再添加一层,这样我就可以指定字母是辅音字母还是元音字母。
我希望能够 运行 我的二元组和三元组比较辅音与元音而不是字母表中的每个字母,但我不知道该怎么做。
我不确定你到底想做什么,但我认为嵌套字典方法不如使用组合字符串键入的平面字典那样干净(即 d['ab']
d['a']['b']
)。我还输入代码来检查 bigram/trigram 是否仅由 vowels/consonants 或混合物组成。
代码:
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})
假设您需要对元音和辅音的顺序进行计数,您可以简单地保留一个不同的映射。
如果你有一个函数 is_vowel(letter)
returns 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