1
我有问题,追加字典,这种状况的时候,关键 字典一样的,这是代码的Python:如何附加型字典,相同的密钥
import random
temp_data = {}
def data_value():
data = []
for x in range (1,6):
val = random.randrange(0, 101, 2)
data.append(val)
return data
for x in range(1,11):
if x == 3:
temp_data.update({ "name" : "Cat" , "total" : data_value() })
if x == 4:
temp_data.update({ "name" : "Dog" , "total" : data_value()})
if x == 5:
temp_data.update({ "name" : "Chicken" , "total" : data_value()})
print temp_data
输出中会
{'total': [20, 84, 10, 14, 10], 'name': 'Chicken'}
我想让输出像
{'total': [20, 84, 10, 14, 10], 'name': 'Cat'},
{'total': [20, 84, 10, 14, 10], 'name': 'Dog'},
{'total': [20, 84, 10, 14, 10], 'name': 'Chicken'}
下摆,我试着追加,但它不能,因为数据必须是数组列表。 谢谢。