Collections.namedtuple()

Sort by

recency

|

1085 Discussions

|

  • + 0 comments
    from collections import namedtuple
    n, code = int(input()), namedtuple("code",input().split())
    print(round(sum([int(code(*input().split()).MARKS) for _ in range(n)])/n,2))
    
  • + 0 comments

    from collections import namedtuple n=int(input())#number of students Student=namedtuple('Student',input())#read no.of column headers like ID,MARKS,NAME,CLASS marks=[]#empty list to collect marks for i in range(n): data=input().split()#read student details data=["1","97"] student=Student(*data)#create student object Student=Student("1","97") marks.append(int(student.MARKS))#take marks and store it in a list print(sum(marks)/n)

  • + 0 comments
    count,Student=int(input()),namedtuple('Student',input().split())
    total_marks=0
    for _ in range(count):
            st=Student(*input().split())
            total_marks+=int(st.MARKS)
    print(total_marks/count)
    
  • + 0 comments

    Something I learned while solving this is that namedtuples have a ._make method that if the input string is in the same order as the namedtuple defintion string, it does the right thing:

    Record = namedtuple('Record', headers)
    :
    :
    read_input = input().split()
    read_record = Record._make(read_input)
    
  • + 0 comments

    Enter your code here. Read input from STDIN. Print output to STDOUT

    from collections import namedtuple n=input() student=namedtuple('student',input().split()) Students=[student(*input().split()) for _ in range(int(n))] print(round(sum(int(i.MARKS) for i in Students)/int(n),2))