使用“ for”循环将字典式添加到字典中

时间:2019-05-28 17:18:31

标签: python dictionary for-loop

以下是列表:

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]}

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']

任何评论将不胜感激.. !!

2 个答案:

答案 0 :(得分:0)

ffplay -loglevel trace -f lavfi -i rgbtestsrc=r=1:d=1:size=800x600 -vf "format=pix_fmts=rgba,drawbox=x=0:y=0:w='iw/2':h='ih/2':t=max:c=white@0.5" 行用for循环后的最后一个元素更新字典,因此最后一个元素出现两次。

删除该行,您的代码将正常工作

因此代码更改为

verb.update(verb[i])

您还可以通过重构字典然后使用列表推导来计算最终列表来简化代码

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)

答案 1 :(得分:0)

您正在循环外的最后更新字典,这会导致最后一个值的重复。删除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)

更好的方法是使用内置的zipenumerate,例如:

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))}