Assume you have a csv file named numberparty.csv that does the following all the
ID: 3888917 • Letter: A
Question
Assume you have a csv file named numberparty.csv that does the following all the way to the last row in excell(integer only goes 1-10):
identifier |integer| name| +1
1 | 5 | john | pete
2 | 3 | mike | michelle
3 | 5 | aaron| julia
4 | 9| leon | train
5 | 6| kim | timmy
6 | 9| maria| vance
7 | 9| tom | bucket
8 | 3| steve| mark
how would we in python sort the integer column numbers so that all the 3's were grouped together in the excel spread sheet, how would we do that also with the 5's being grouped and so on.... Also how would you make it so that each segment where the numbers were like were put into another file so all the 1's were put together and the 2's and 3's and so on...
Explanation / Answer
import csv import operator sample=open('numberparty.csv', "r") data=csv.reader(sample, delimiter='|') header=next(csv1, None) sort=sorted(data, key = lambda x: int(x[1])) The lambda is a function that picks the second element of each sub-list as the key, i.e. the integer, and converts it to a number for sorting
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.