Sort CSV using a key computed from two columns, grab first n largest values

后端 未结 3 505
被撕碎了的回忆
被撕碎了的回忆 2021-01-17 03:11

Python amateur here...let\'s say here I have snippet of an example csv file:

Country, Year, GDP, Population
Country1         


        
3条回答
  •  没有蜡笔的小新
    2021-01-17 03:34

    Use the optional key argument to the sort function:

    array.sort(key=lambda x: x[2])
    

    will sort array using its third element as a key. The value of the key argument should be a lambda expression that takes in a single argument (an arbitrary element of the array being sorted) and returns the key for sorting.

    For your GDP example, the lambda function to use would be:

    lambda x: float(x[2])/float(x[3]) # x[2] is GDP, x[3] is population
    

    The float function converts the CSV fields from strings into floating point numbers. Since there are no guarantees that this will be successful (improper formatting, bad data, etc), I'd typically do this before sorting, when inserting stuff into the array. You should use floating point division here explicitly, as integer division won't give you the results you expect. If you find yourself doing this often, changing the behavior of the division operator is an option (http://www.python.org/dev/peps/pep-0238/ and related links).

提交回复
热议问题