'Python: sort list except first line
I have this list :
[['Nom', 'Francais', 'Anglais', 'Maths'], ['Catherine', '9', '17', '9'], ['Karim', '12', '15', '11'], ['Rachel', '15', '15', '14'], ['Roger', '12', '14', '12'], ['Gabriel', '7', '13', '8'], ['Francois', '14', '8', '15'], ['Henri', '10', '12', '13'], ['Stephane', '18', '12', '8'], ['Karine', '9', '10', '10'], ['Marie', '10', '10', '10'], ['Claire', '15', '9', '12'], ['Marine', '12', '9', '12']]
I want to sort it with the names (or, in another words, by alphabetical order of the [0] element of each list within the list) but i don't want don't want the first list (['Nom', 'Francais', 'Anglais', 'Maths']) to be sorted with the others , how can in do that ?
Thanks a lot !
Solution 1:[1]
You can use range assignment:
>>> from pprint import pprint # just to have a nice display
>>> data = [['Nom', 'Francais', 'Anglais', 'Maths'], ['Catherine', '9', '17', '9'], ['Karim', '12', '15', '11'], ['Rachel', '15', '15', '14'], ['Roger', '12', '14', '12'], ['Gabriel', '7', '13', '8'], ['Francois', '14', '8', '15'], ['Henri', '10', '12', '13'], ['Stephane', '18', '12', '8'], ['Karine', '9', '10', '10'], ['Marie', '10', '10', '10'], ['Claire', '15', '9', '12'], ['Marine', '12', '9', '12']]
>>> pprint(data)
[['Nom', 'Francais', 'Anglais', 'Maths'],
['Catherine', '9', '17', '9'],
['Karim', '12', '15', '11'],
['Rachel', '15', '15', '14'],
['Roger', '12', '14', '12'],
['Gabriel', '7', '13', '8'],
['Francois', '14', '8', '15'],
['Henri', '10', '12', '13'],
['Stephane', '18', '12', '8'],
['Karine', '9', '10', '10'],
['Marie', '10', '10', '10'],
['Claire', '15', '9', '12'],
['Marine', '12', '9', '12']]
>>> data[1:] = sorted(data[1:])
>>> pprint(data)
[['Nom', 'Francais', 'Anglais', 'Maths'],
['Catherine', '9', '17', '9'],
['Claire', '15', '9', '12'],
['Francois', '14', '8', '15'],
['Gabriel', '7', '13', '8'],
['Henri', '10', '12', '13'],
['Karim', '12', '15', '11'],
['Karine', '9', '10', '10'],
['Marie', '10', '10', '10'],
['Marine', '12', '9', '12'],
['Rachel', '15', '15', '14'],
['Roger', '12', '14', '12'],
['Stephane', '18', '12', '8']]
Solution 2:[2]
Personally, I'd do something like this. But it assumes you're semi-comfortable with Pandas. This gives you a lot more flexibility to do more with the data.
import pandas as pd
nl = [['Nom', 'Francais', 'Anglais', 'Maths'], ['Catherine', '9', '17', '9'], ['Karim', '12', '15', '11'], ['Rachel', '15', '15', '14'], ['Roger', '12', '14', '12'], ['Gabriel', '7', '13', '8'], ['Francois', '14', '8', '15'], ['Henri', '10', '12', '13'], ['Stephane', '18', '12', '8'], ['Karine', '9', '10', '10'], ['Marie', '10', '10', '10'], ['Claire', '15', '9', '12'], ['Marine', '12', '9', '12']]
df = pd.DataFrame(columns = nl[0])
for l, c in zip(nl[0], range(4)):
df[l] = [ r[c] for r in nl[1:] ]
df.sort_values(by = 'Nom', inplace = True)
df.reset_index(drop = True, inplace = True)
which yields:
Nom Francais Anglais Maths
0 Catherine 9 17 9
1 Claire 15 9 12
2 Francois 14 8 15
3 Gabriel 7 13 8
4 Henri 10 12 13
5 Karim 12 15 11
6 Karine 9 10 10
7 Marie 10 10 10
8 Marine 12 9 12
9 Rachel 15 15 14
10 Roger 12 14 12
11 Stephane 18 12 8
and then if you need a .csv per your most recent comment, it's simply:
df.to_csv('/directory/my_filename.csv', index = False)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Valentin Lorentz |
| Solution 2 | elPastor |
