A
A
Andrew2016-04-27 17:07:33
Python
Andrew, 2016-04-27 17:07:33

Can't figure out why list.append doesn't work as expected?

Good day!
Based on the question
tried to solve but ran into difficulty when working with list.append
python 3.4

students_data = [
    {'Name': 'Vasya', 'marks': [4, 5, 4, 5, 5]},
    {'Name': 'Petya', 'marks': [3, 5, 3, 4, 2, 2]},
    {'Name': 'Ivan', 'marks': [4, 5, 4, 4, 4, 4]},
    {'Name': 'Dima', 'marks': [4, 5, 4, 3, 3, 3]},
]

average_data = []
student_struct = {}
summ = 0

for student in students_data:
    all_marks = student['marks']
    individual_name = student['Name']

    for individual_mark in all_marks:
        summ += individual_mark

    student_struct['name'] = individual_name
    student_struct['average'] = summ
    average_data.append(student_struct)

    print('  ')
    print(student_struct)
    print(average_data)

    print(individual_name, end=' ')
    print(summ)

print(average_data)

I added these lines to understand how the code works:
print('  ')
    print(student_struct)
    print(average_data)

    print(individual_name, end=' ')
    print(summ)

I expect the contents of student_struct to be added to the average_data list in the loop.
Which in turn contains the Name and the total number of points.
those. something like:
[{'average': 23, 'name': 'Vasya'}, {'average': 42, 'name': 'Petya'}, {'average': 67, 'name': 'Ivan'}, { 'average': 89, 'name': 'Dima'}]

But I get this:
{'average': 23, 'name': 'Vasya'}
[{'average': 23, 'name': 'Vasya'}]
Vasya 23
{'average': 42, 'name': 'Petya'}
[ {'average': 42, 'name': 'Petya'}, {'average': 42, 'name': 'Petya'}]
Petya 42
{'average': 67, 'name': 'Ivan'}
[ {'average': 67, 'name': 'Ivan'}, {'average': 67, 'name': 'Ivan'}, {'average': 67, 'name': 'Ivan'}]
Ivan 67
{'average': 89, 'name': 'Dima'}
[{'average': 89, 'name': 'Dima'}, {'average': 89, 'name': 'Dima'}, {' average': 89,'name': 'Dima'}, {'average': 89, 'name': 'Dima'}]
Dima 89
[{'average': 89, 'name': 'Dima'}, {'average': 89 , 'name': 'Dima'}, {'average': 89, 'name': 'Dima'}, {'average': 89, 'name': 'Dima'}]

I have been learning Python not so long ago, but I considered myself an advanced beginner, and now I am disappointed in my knowledge of the language.
Can you help me figure out what I'm writing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2016-04-27
@deepblack

1. You didn't say what task.
2. I will be Vanga today:
a)

student_struct = {}
summ = 0

I think this should be included in the cycle.
b) There is a sum function that calculates the sum of an iterable object. Moreover, it is much faster than the python implementation. I suspect the code
summ = 0
for individual_mark in all_marks:
        summ += individual_mark

Can be replaced with one line sum(all_marks)
c) all_marks = student['marks']
This duplication is unclear for what at all. You can remove it altogether.
d)
student_struct = {}
student_struct['name'] = individual_name
student_struct['average'] = summ

This is also reduced to
student_struct = {
    'name': student['Name'],
    'average': sum(student['marks'])
}

e) It is not clear why the dictionary has a key with a capital letter. Causes a lot of confusion.
The result is something like this:
students_data = [
    {'name': 'Vasya', 'marks': [4, 5, 4, 5, 5]},
    {'name': 'Petya', 'marks': [3, 5, 3, 4, 2, 2]},
    {'name': 'Ivan', 'marks': [4, 5, 4, 4, 4, 4]},
    {'name': 'Dima', 'marks': [4, 5, 4, 3, 3, 3]},
]

average_data = [{'name': student['name'], 'average': sum(student['marks'])} for student in students_data]

PS It is not clear why the sum is called average.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question