Home > database >  average of specific values of list of dictionary values python
average of specific values of list of dictionary values python

Time:10-31

Hey I'm trying to get the average of values from a list the list has values from a dictionary I keep returning this error

all_students_avg = sum(numbers_lists) / len(numbers_lists)

TypeError: unsupported operand type(s) for : 'int' and 'list'

I don't understand, the values I'm calling are are integers already and I've tried converting them to integers and float.

Bob = {'name': 'bob', 'assignments': 80.0, 'presentation': 80.0, 'lab tasks': 60.0}
Geoff = {'name': 'Geoff', 'assignments': 90.0, 'presentation': 90.0, 'lab tasks': 90.0}
numbers_lists = [Bob['assignments'], Bob['presentation'], Bob['lab tasks']], [Geoff['assignments'], "\n",
                                                                          Geoff['presentation'],
                                                                          Geoff['lab tasks']]
all_students_avg = sum(numbers_lists) / len(numbers_lists)
print("This is all the students average score across all subjects: ", all_students_avg, )

CodePudding user response:

Python list has a format like: l = [element1, element2, element3]. Your list was looking like: numbers_lists = [[element1, element2], "\n", [elem1, elem2]] because your list not only include two lists but it also contains a newline character that can't be summed with integer.

I made a little modification to your code and this should work:

Bob = {'name': 'bob', 'assignments': 80.0, 'presentation': 80.0, 'lab tasks': 60.0}
Geoff = {'name': 'Geoff', 'assignments': 90.0, 'presentation': 90.0, 'lab tasks': 90.0}
bob_scores = [Bob['assignments'], Bob['presentation'], Bob['lab tasks']]
geoff_scores = [Geoff['assignments'], Geoff['presentation'], Geoff['lab tasks']]
                                                                                                                                           
numbers_lists = bob_scores   geoff_scores
all_students_avg = sum(numbers_lists) / len(numbers_lists)
print("This is all the students average score across all subjects: ", all_students_avg)


output:

This is all the students average score across all subjects:  81.66666666666667

CodePudding user response:

The reason you're getting an error is because the variable numbers_lists is in list format.
You may try this

print(numbers_lists)

Inorder to handle this problem, you can convert all elements to integers using map().

num_list = list(map(int,numbers_lists))

CodePudding user response:

Your list structure is not going to work as you expect it to. The list you have will need to be unpacked as seen here https://www.w3schools.com/python/python_tuples_unpack.asp.

You could add extra brackets to the ends to make a list of multiple lists.

Bob = {'name': 'bob', 'assignments': 80.0, 'presentation': 80.0, 'lab tasks': 60.0}
Geoff = {'name': 'Geoff', 'assignments': 90.0, 'presentation': 90.0, 'lab tasks': 90.0}
# if you want 2 seperate lists you could "unpack" them like this
numbers_lists1, number_lists2 = [Bob['assignments'], Bob['presentation'], Bob['lab tasks']], [Geoff['assignments'], "\n",
                                                                          Geoff['presentation'],
                                                                          Geoff['lab tasks']]
print(numbers_lists1)
print(number_lists2)

# if you want a list with 2 lists in it you will need to add extra [] on the ends
fixed_numbers_lists = [[Bob['assignments'], Bob['presentation'], Bob['lab tasks']], [Geoff['assignments'], "\n",
                                                                          Geoff['presentation'],
                                                                          Geoff['lab tasks']]]
print(fixed_numbers_lists)
  • Related