0
我有一個列表python-多維列表,並創建一個字典,並計算其平均
這樣
myList = [ ['jan','423','523','645'],
['jan','654','754','765'],
['nov','756','087','140'],
['nov','233','123','032']
['apr','654','786','223'] ]
我想通過列表和羣組的所有基於子列表(myList中的)第一個元素(即'jan','nov')在字典中。 其關鍵也是列表的第一個元素。
myDict = {'jan':[
['423','654'], # average
['523','754'], # average
['645','765'] # average
],
'nov':[
['756','233'], # average
['087','123'], # average
['140','032'] # average
],
'apr':[
['654'], # average
['786'], # average
['223'] # average
],
}
然後我想計算該字典的所有列表元素的平均值。
這樣
myDict = {'jan':[
['423','654'], # average
['523','754'], # average
['645','765'] # average
],.....
注意:這僅僅是樣本數據,我有數百elemnts的像 子列表[34,654,756,8,675,75,64,3,45,... .n],[sublist-2..n],[sublist-3 .... n]] ,但每個子列表的長度是固定的。
mycode的:
myList
db = {}
for i in myList:
human = i[0]
newlist = i[1:]
# print pprint(db)
columns = []
counter = 0
while counter < len(newlist):
if db.has_key(human):
db[human][counter].append(newlist[counter])
else:
columns.append([newlist[counter]])
db[human] = columns
counter += 1
下面列出的代碼工作良好,當我只有2項。
human = i[0]
col_1 = i[1]
col_2 = i[2]
if db.has_key(human):
db[human][0].append(col_1)
db[human][1].append(col_2)
else:
db[human] = [ [col_1], [col_2] ]
print
pprint(db)
print columns
# function to calculate average
def getAvg(column):
total = 0
average = 0
for val in column:
length = len(column)
int_val = float(val)
total += int_val
average = total/length
return average
col_1_avg = getAvg(col_1)
col_2_avg = getAvg(col_2)
result = human + ',' + str(getAvg(col_1)) + ',' +str(getAvg(col_2))
看http://stackoverflow.com/questions/21674331/group-by-multiple-keys-and-summarize-average-values-of-a-list-of-dictionaries/21674941 #21674941 –
請不要使用'dict.has_key'來檢查密鑰的存在,它已被棄用。在my_dict中使用'if k:...' –