Answer the question
In order to leave comments, you need to log in
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)
print(' ')
print(student_struct)
print(average_data)
print(individual_name, end=' ')
print(summ)
[{'average': 23, 'name': 'Vasya'}, {'average': 42, 'name': 'Petya'}, {'average': 67, 'name': 'Ivan'}, { 'average': 89, 'name': 'Dima'}]
{'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'}]
Answer the question
In order to leave comments, you need to log in
1. You didn't say what task.
2. I will be Vanga today:
a)
student_struct = {}
summ = 0
summ = 0
for individual_mark in all_marks:
summ += individual_mark
sum(all_marks)
all_marks = student['marks']
student_struct = {}
student_struct['name'] = individual_name
student_struct['average'] = summ
student_struct = {
'name': student['Name'],
'average': sum(student['marks'])
}
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]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question