Python's 2D list. I have a question.

Asked 2 years ago, Updated 2 years ago, 94 views

Hello, I'm going to create a function that takes the mean from the list in the list.


 grades = [
             ['Student', 'Quiz 1', 'Quiz 2', 'Quiz 3'],
             ['Joe', 100, 90, 80],
             ['McD', 88, 99, 111],
             ['Ruta', 50, 56, 67],
             ['Kang', 60, 62, 67],
             ['Samanda', 70, 79, 88],
             ['Mina', 98, 99, 100]]

I'll explain the first list From the second list, it contains data. And the first data in the second list is string.

I'm not used to coding, so I don't know where to start... I can't understand even if I read about the 2D list on the Internet.lol

average = sum(maria.values()) / len(maria)

Even if I tried to subtract the number using value(), it didn't work out as I wanted How should I approach it?

python list def

2022-09-22 17:55

4 Answers

How to use Pandas.

>>> import pandas as pd

>>> grades = [
             ['Student', 'Quiz 1', 'Quiz 2', 'Quiz 3'],
             ['Joe', 100, 90, 80],
             ['McD', 88, 99, 111],
             ['Ruta', 50, 56, 67],
             ['Kang', 60, 62, 67],
             ['Samanda', 70, 79, 88],
             ['Mina', 98, 99, 100]]
>>> df = pd.DataFrame(grades[1:], columns=grades[0])
>>> df
   Student  Quiz 1  Quiz 2  Quiz 3
0      Joe     100      90      80
1      McD      88      99     111
2     Ruta      50      56      67
3     Kang      60      62      67
4  Samanda      70      79      88
5     Mina      98      99     100
>>> df['Average'] = df.apply(lambda row:(row['Quiz 1']+row['Quiz 2']+row['Quiz 3'])/3, axis=1)
>>> df
   Student  Quiz 1  Quiz 2  Quiz 3    Average
0      Joe     100      90      80  90.000000
1      McD      88      99     111  99.333333
2     Ruta      50      56      67  57.666667
3     Kang      60      62      67  63.000000
4  Samanda      70      79      88  79.000000
5     Mina      98      99     100  99.000000
>>> 


2022-09-22 17:55

Method using dictionary

grads= []

grades.append({'student':'Joe', 'Quiz 1':100, 'Quiz 2':90, 'Quiz 3':80})
grades.append({'student':'McD', 'Quiz 1':88, 'Quiz 2':99, 'Quiz 3':111})
grades.append({'student':'Ruta', 'Quiz 1':50, 'Quiz 2':56, 'Quiz 3':67})
grades.append({'student':'Kang', 'Quiz 1':60, 'Quiz 2':62, 'Quiz 3':67})
grades.append({'student':'Samanda', 'Quiz 1':70, 'Quiz 2':79, 'Quiz 3':88})
grades.append({'student':'Mina', 'Quiz 1':98, 'Quiz 2':99, 'Quiz 3':100})

for list in grades:
    print ('average of student', list.get('student'), (list.get('Quiz 1') +list.get('Quiz 2')+list.get('Quiz 3'))/3 )

>>>
average of student Joe 90.0
average of student McD 99.33333333333333
average of student Ruta 57.666666666666664
average of student Kang 63.0
average of student Samanda 79.0
average of student Mina 99.0


2022-09-22 17:55

The dictionary compression is available as follows.

grades = [
             ['Student', 'Quiz 1', 'Quiz 2', 'Quiz 3'],
             ['Joe', 100, 90, 80],
             ['McD', 88, 99, 111],
             ['Ruta', 50, 56, 67],
             ['Kang', 60, 62, 67],
             ['Samanda', 70, 79, 88],
             ['Mina', 98, 99, 100]]

{row[0] : sum(row[1:]) / len(row[1:]) for row in grades[1:]}

{'Joe': 90.0,
 'McD': 99.33333333333333,
 'Ruta': 57.666666666666664,
 'Kang': 63.0,
 'Samanda': 79.0,
 'Mina': 99.0}


2022-09-22 17:55

I calculated it by referring to the given list without changing it to pandas or dictionary.

This is the result of running on Jupiter laptop.


2022-09-22 17:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.