sorting - Split many lists into 2 sections which contain strings and numbers -
i have csv file looks below:
hussain 1 0 0 3
i have managed make of them lists created in rows: lists produced follows:
['hussain', '1', '0', '0', '3']
i want able sort them names printed alphabetically along each person's greatest score: note 1st number excluded student's class number out of 1, 2 , 3 , not part of score. people in specific class need printed user decide class sort:
if user says class 1 program should print:
hussain:3
i appreciate have been struggling on part of code though have been working on long period of time. thank you
try this:
tuples = [['hussain', '1', '0', '0', '3'], ['bon', '2', '1', '2', '3'], ['alice', '1', '5', '2', '3'], ['josh', '3', '1', '7', '10'], ['jack', '2', '0', '8', '9'], ['zyra', '3', '5', '1', '6']] class_to_sort = 1 # reduce classes requested user tuples = filter(lambda tuple: int(tuple[1]) == class_to_sort, tuples) # sort tuples tuples = sorted(tuples, key=lambda tuple: tuple[0]) # throw out second column , converts strings integers tuples = [[tuple[0]] + map(int, tuple[2:]) tuple in tuples] # throw out integers aren't maximum per tuple tuples = [[tuple[0]] + [max(tuple[1:])] tuple in tuples] print tuples
outputs list of [name, max score], can use want:
[['alice', 5], ['hussain', 3]]