我有一个 4 克的列表,我想用它填充字典对象/舍夫勒对象:
['I','go','to','work']
['I','go','there','often']
['it','is','nice','being']
['I','live','in','NY']
['I','go','to','work']
这样我们就有了这样的东西:
four_grams['I']['go']['to']['work']=1
任何新遇到的 4-gram 都会填充它的四个键,值为 1,如果再次遇到它,其值会增加。
你可以做这样的事情:
import shelve
from collections import defaultdict
db = shelve.open('/tmp/db')
grams = [
['I','go','to','work'],
['I','go','there','often'],
['it','is','nice','being'],
['I','live','in','NY'],
['I','go','to','work'],
]
for gram in grams:
path = db.get(gram[0], defaultdict(int))
def f(path, word):
if not word in path:
path[word] = defaultdict(int)
return path[word]
reduce(f, gram[1:-1], path)[gram[-1]] += 1
db[gram[0]] = path
print db
db.close()
您可以创建一个帮助程序方法,该方法一次将元素插入一个嵌套字典中,每次检查所需的子字典是否已存在:
dict = {}
def insert(fourgram):
d = dict # reference
for el in fourgram[0:-1]: # elements 1-3 if fourgram has 4 elements
if el not in d: d[el] = {} # create new, empty dict
d = d[el] # move into next level dict
if fourgram[-1] in d: d[fourgram[-1]] += 1 # increment existing, or...
else: d[fourgram[-1]] = 1 # ...create as 1 first time
您可以使用数据集填充它,例如:
insert(['I','go','to','work'])
insert(['I','go','there','often'])
insert(['it','is','nice','being'])
insert(['I','live','in','NY'])
insert(['I','go','to','work'])
之后,您可以根据需要索引到dict
:
print( dict['I']['go']['to']['work'] ); # prints 2
print( dict['I']['go']['there']['often'] ); # prints 1
print( dict['it']['is']['nice']['being'] ); # prints 1
print( dict['I']['live']['in']['NY'] ); # prints 1