[英]Adding dictonary into the dictonary using 'for' loop
以下是列表:
text = ['is', 'ramping', 'brings']
head = ['ramping', 'ramping', 'ramping']
childWord = [[],
['Cola', 'is', 'up', 'launches', 'brings', 'drink', '.'],
['as', 'it', 'mineral']]
並使用下面的代碼我更新這個列表項目的dictonary動詞的值。
verb = {}
for i in range(0 , len(text)):
verb [i] = {'Verb Text': text[i], 'Text Head of the verb' : head[i], 'Child Word of the verb' : childWord[i]}
verb.update(verb [i])
verb
我得到的輸出如下:
{0: {'Verb Text': 'is',
'Text Head of the verb': 'ramping',
'Child Word of the verb': []},
1: {'Verb Text': 'ramping',
'Text Head of the verb': 'ramping',
'Child Word of the verb': ['Cola',
'is',
'up',
'launches',
'brings',
'drink',
'.']},
2: {'Verb Text': 'brings',
'Text Head of the verb': 'ramping',
'Child Word of the verb': ['as', 'it', 'mineral']},
'Verb Text': 'brings',
'Text Head of the verb': 'ramping',
'Child Word of the verb': ['as', 'it', 'mineral']}
在輸出中,問題是它創建了兩次鍵值對
'Verb Text': 'brings',
'Text Head of the verb': 'ramping',
'Child Word of the verb': ['as', 'it', 'mineral']
任何評論將不勝感激.. !!
行verb.update(verb[i])
使用for循環后的最后一個元素更新字典,因此最后一個元素出現兩次。
刪除該行,您的代碼將正常工作
因此代碼變為
text = ['is', 'ramping', 'brings']
head = ['ramping', 'ramping', 'ramping']
childWord = [[],
['Cola', 'is', 'up', 'launches', 'brings', 'drink', '.'],
['as', 'it', 'mineral']]
verb = {}
for i in range(0 , len(text)):
verb [i] = {'Verb Text': text[i], 'Text Head of the verb' : head[i], 'Child Word of the verb' : childWord[i]}
#Remove this line since it is updating the dictionary with the last element again
#verb.update(verb [i])
print(verb)
您還可以通過重新構建字典然后使用列表推導來計算最終列表來簡化代碼
#Dictionary of keys to list of values
dct = { 'Verb Text': ['is', 'ramping', 'brings'],
'Text Head of the verb': ['ramping', 'ramping', 'ramping'],
'Child Word of the verb': [[],
['Cola', 'is', 'up', 'launches', 'brings', 'drink', '.'],
['as', 'it', 'mineral']]
}
#Keys of the dictionary
keys = dct.keys()
#Iterate over the indexes and keys to make your list via list and dictionary comprehension
verb = [{key:dct[key][idx]} for idx in range(len(dct['Verb Text'])) for key in keys]
print(verb)
並且在兩種情況下輸出都是相同的
{
0: {'Verb Text': 'is', 'Text Head of the verb': 'ramping', 'Child Word of the verb': []},
1: {'Verb Text': 'ramping', 'Text Head of the verb': 'ramping', 'Child Word of the verb': ['Cola', 'is', 'up', 'launches', 'brings', 'drink', '.']},
2: {'Verb Text': 'brings', 'Text Head of the verb': 'ramping', 'Child Word of the verb': ['as', 'it', 'mineral']}
}
您正在循環外的末尾更新字典,這導致重復最后一個值。 刪除dict.update
外部循環:
verb = {}
for i in range(0 , len(text)):
verb[i] = {'Verb Text': text[i], 'Text Head of the verb' : head[i], 'Child Word of the verb' : childWord[i]}
#verb.update(verb [i]) <- Remove this line
print(verb)
更好的方法是使用內置的zip
並enumerate
如下:
verb = {}
for i, (x, y, z) in enumerate(zip(text, head, childWord)):
verb[i] = {'Verb Text': x, 'Text Head of the verb': y, 'Child Word of the verb': z}
print(verb)
再次使用字典理解:
verb = {i: {'Verb Text': x, 'Text Head of the verb': y, 'Child Word of the verb': z} for i, (x, y, z) in enumerate(zip(text, head, childWord))}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.