Sample Input
20, 71146620
100, 26867616
10, 02513583
10, 52811698
100, 23859051
I read it in from a file as a command line argument to a list with
lin = [i.strip() for i in open(sys.argv[1]).readlines()]
that list looks like ['20, 71146620', '100, 26867616', '10, 02513583', '10, 52811698', '100, 23859051']
My hope is to find the most pythonic way to sort this list, first for the first value and then for the second so that it looks like:
['10, 02513583', '10, 52811698', '20, 71146620', '100, 23859051', '100, 26867616', ]
I'm currently trying to convert it to a list of key value pairs, but I'm not sure that's the right direction to take.
解决方案
The easiest thing to do is to parse the pairs into lists and then just sort them:
lin = [i.strip().split(', ') for i in open(sys.argv[1]).readlines()]
lin = sorted(lin)
In case you want to sort numerically, just cast to numbers:
lin = [map(int, i.strip().split(', ')) for i in open(sys.argv[1]).readlines()]
lin = sorted(lin)