Hab noch ein Problem mit der Zusammenfassung von Dictionnaries in einer Liste:
Folgende Liste, mit verschiedenen Dictinaries hab ich vorliegen:
Code: Alles auswählen
dict_list =
[{'ID': '1', 'Width': ' 3.000', 'Weight': ' 322.378', 'Area': ' 18.523'},
{'ID': '1', 'Width': ' 4.000', 'Weight': ' 352.338', 'Area': ' 18.543'},
{'ID': '2', 'Width': ' 3.000', 'Weight': ' 362.318', 'Area': ' 18.233'},
{'ID': '2', 'Width': ' 2.000', 'Weight': ' 312.328', 'Area': ' 18.133'},
{'ID': '2', 'Width': ' 1.000', 'Weight': ' 322.368', 'Area': ' 18.233'},
{'ID': '2', 'Width': ' 1.000', 'Weight': ' 322.368', 'Area': ' 18.233'},
{'ID': '3', 'Width': ' 1.000', 'Weight': ' 322.368', 'Area': ' 18.233'},
{'ID': '3', 'Width': ' 1.000', 'Weight': ' 322.368', 'Area': ' 18.233'}]
Diese Konstruktion scheitert, weil die IDs nicht richtig zugeordnet sind:
Code: Alles auswählen
weights = []
helpvar=''
for i in range(len(dict_list)):
dict1 = dict_list[i]
if helpvar != dict1.get('ID'):
weights.append(float(dict1.get('Weight')))
print 'Weight: ', dict1.get('Weight'), dict1.get('ID')
print 'Summe:', sum(weights)
print '\n'
weights = [] # Leeren der Gewichtsliste
else:
print 'Weight: ', dict1.get('Weight'), dict1.get('ID')
weights.append(float(dict1.get('Weight')))
helpvar = dict1.get('ID') # Problemursprung?
print 'Summe: ', sum(weights)
Danke,
Johannes