Python count and average value of keys in list of dict -
i have file people_id , room_id column. read file dict.
people_id room_id 1   8 2   32 3   8 4   47 5   12 6   8   and code
report_keys = ['people_id', 'room_id'] report = [] open("file.txt") f:     line in f:         line = line.strip().split('\t')         d = dict(zip(report_keys, line))         report.append(d)   i count people per room. eg. room_id 8 number of people in room = 3 , average of people in room's.
output:
room_id 8 = 3 people room_id 32 = 1 people room_id 47 = 1 people room_id 12 = 1 people   and average number of people checked in 1 room.
i try
for key, value in report:     print(key, len([item item in value if item]))   but code print same values
i think can change code how read file
report = {} open("file.txt") f:     line in f:         line = line.strip().split('\t')         report[line[1]] = report.get(line[1],0) + 1      
Comments
Post a Comment