Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sum all the elements (except for the first one) in rows in 2d list?

So, my task is to write a program which gets an integer 𝑛 on the input, then 𝑛 times asks for the student’s name (one word) and a space-separated list of their grades, and on the output there should be a list of tuples, where each tuple consists of the student’s name and their grade point average, rounded to integers.

Example:

Input:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

3

Ann 5 6 7 8

Ben 10 10 0 0

Fred 7 6 8 5

Output:

[(‘Ann’, 6), (‘Ben’, 5), (‘Fred’, 6)]

Now my program looks like this:

n = int(input())
a = list()
for i in range(n):
  a.append(input().split())
print(tuple(a))

I have no idea how to sum numbers in each row (except for the first string, student’s name) and make it like a new list. Maybe I should somehow use numpy in this case, but still don’t know how. I will be grateful for any help

>Solution :

  • After splitting each line on a space, the first element of the result is the name, and the rest of the elements are the grades.
  • Use sum to get the total of all grades, then divide by the number of grades to get the average.
n = int(input())
a = []
for i in range(n):
  name, *grades = input().split()
  a.append((name, round(sum(map(int, grades)) / len(grades))))
print(a)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading