📜  如何在 for 循环 python 代码示例中将新项目添加到字典中

📅  最后修改于: 2022-03-11 14:45:59.340000             🧑  作者: Mango

代码示例1
'''
There are 5 items with list as a key. To add a new item 'F' to the dictionary
you would want to use list(d.items()) in for loop
'''

d = {'A': [1, 5, 6, 7], 'B':[4, 5, 6, 7, 9], 'C':[12 ,4, 6, 3, 6], 'D':[23, 5, 7, 23, 12], 'E':[22, 2, 1, 4, 6]}
i = 1 #just for conditional
asc_ltr = 70 #asci value for letter 'F'

for k, v in list(d.items()):
    if i == 3:
        d['A'].append(50) #just checking the extra condition
    else: 
        #to create a new key and value 
        d.update({chr(asc_ltr): [np.mean(v), 20, 30, 40]})
    i += 1