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'}
下襬,我試着追加,但它不能,因爲數據必須是數組列表。 謝謝。