简体   繁体   中英

Summing up a list in a dictionary

I want to sum up the points inside a For loop. As you can see in my code, the number of elements (entries) in "points" is different. Can someone help me to implement this pytonically?

student1 = {'name': 'Hans', 'points': [285, 210, 135, 100, 300]} student2 = {'name': 'Peter', 'points': [65, 56, 48]}

students = [student1, student2]

for stud in students: sumpoints = stud['points'][0]+stud['points'][1]+stud['points'][2]

print(sumpoints)]

The print output should look like this:

Hans: 1030
Peter: 169

That's why I put it in a for loop.

What you have

Your current code prints:

169

Explain your code and intentions by comments:

student1 = {'name': 'Hans', 'points': [285, 210, 135, 100, 300]}
student2 = {'name': 'Peter', 'points': [65, 56, 48]}

students = [student1, student2]

# Print a line for each student with his name and the sum of his/her points.
# Example:
# Hans: 1030
# Peter: 169
for stud in students:
    sumpoints = stud['points'][0]+stud['points'][1]+stud['points'][2]  # this will not adjust for varying list-length

print(sumpoints)  # this is outside the for-loop, it only prints the sum of the last student

What is missing?

  • a line printed per student (inside the loop)
  • the name of the student at the beginning of the printed
  • the sum per student should summarize all points not only the first 3
for stud in students:
    # following debug prints can be removed when working correctly
    print(stud['name'])  # see how each student is iterated - a new line
    print(stud['points'])  # see the list varies (length and elements)
    # the demo can be completed by you
    stud_sum = sum([0,1,2]) # sum-function adjusts for varying list-length, it sums up all elements - here a demo list
    print("Name" + ":" +  str(stud_sum))  # inside the for-loop, to print for each student - the name is just a demo

sum() will sum of iterable of numbers regardless the numbers of element.

You can use dict comprehension to achieve what you need:

EDIT: Changes variable name in dict comprehension for better readability

student1 = {'name': 'Hans', 'points': [285, 210, 135, 100, 300]}
student2 = {'name': 'Peter', 'points': [65, 56, 48]}

students = [student1, student2]

sumpoints = {student['name']: sum(student['points']) for student in students}
print(sumpoints)
for key, val in sumpoints.items(): print(f'{key}: {val}')

# {'Hans': 1030, 'Peter': 169}
# Hans: 1030
# Peter: 169

If you want the total points of all students, it'll look something like this:

sumpoints = 0
for stud in students:
    sumpoints += sum(stud['points'])

The sum function will sum a list without you specifying a size and a list comprehension will apply that to each student.

student1 = {'name': 'Hans', 'points': [285, 210, 135, 100, 300]}
student2 = {'name': 'Peter', 'points': [65, 56, 48]}

students = [student1, student2]

sumpoints = [sum(student['points']) for student in students]
print(sumpoints)

Output is a sum per student

[1030, 169]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM