[英]How to make a list of dicts with nested dicts
我不確定這是否是我真正想要的,但我基本上想要這種結構,
-document
-pattern_key
-start_position
-end_position
現在我有這個
dictionary[document] = {"pattern_key":key, "startPos":index_start, "endPos": index_end}
但是我想在模式鍵下嵌套startPos,endPos
另外我該如何更新它,以在同一文檔下添加pattern_key,startPos,endPos的新條目?
這樣您就可以更新鍵的值
for x in dict.keys():
if 'pattern_key' in x:
dict[x]= 'new value'
if 'endPost'...
.....
print dict
>>> should have new values
用於添加新條目:
old = {}
old['keyxD'] = [9999]
new = {}
new['newnewnew'] = ['supernewvalue']
new.update(old)
print new
>>>{'newnewnew': ['supernewvalue'], 'keyxD': [9999]}
不確定您到底想要什么,但是您可以嘗試以下操作:
document = {'pattern_key' : {}}
這創建了一個目標document
,該document
是一個字典,其中的鍵是字符串(pattern_key),值是另一個字典。 現在,要將新條目添加到字典(這是另一個字典)的值中,只需添加值:
document['pattern_key']['start_position'] = 1
document['pattern_key']['end_position'] = 10
之后,您可以輸入document['pattern_key']['start_position'] = 2
任一情況,這會將開始位置更改為2。
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.