Collections.namedtuple()

Sort by

recency

|

1038 Discussions

|

  • + 0 comments
    from collections import defaultdict
    n = int(input())
    k = defaultdict(list)
    header = list(map(str, input().split()))
    
    listed = []
    for x in range(n):
        listed.append(list(input().split()))
    
    for i, j in enumerate(header):
        for x in range(n):
            if (listed[x][i]).isnumeric():
                k[j].append(int(listed[x][i]))
            else:
                k[j].append(listed[x][i])
    
    print(f'{(sum(k.get('MARKS'))/n):.2f}')
    

    this works even they change colomn order

  • + 0 comments

    total_students, columns = int(input()), input().split() marks = sum(int(input().split()[columns.index('MARKS')]) for _ in range(total_students)) print(f"{marks / total_students:.2f}")

  • + 0 comments

    from collections import namedtuple import statistics

    n,student=int(input()),namedtuple("student",input().split())

    print(statistics.mean(int(student(*(input().split())).MARKS) for _ in range(n)))

  • + 0 comments

    2 lines w/o namedtuple

    iternum, header = int(input()), input().split() print(f'{sum(int(input().split()[header.index("MARKS")]) for _ in range(iternum)) / iternum:.2f}')

  • + 0 comments
    from collections import namedtuple
    import statistics
    
    if __name__ == '__main__':
        total_students,studnet = int(input()),namedtuple('studnet', list(map(str,input().split())))
        print(f'{statistics.mean([int(studnet(*(input().split())).MARKS) for _ in range(total_students)]):.2f}')