[sage-support] Sorting a list of variables

2019-04-21 Thread G. M.-S.
Hello. I would like to sort a list like [x11,x8,x10,x9] so as to get [x8,x9,x10,x11] This does not work, of course: sorted([x11,x8,x10,x9],key=repr) as it gives [x10,x11,x8,x9] Any ideas? Thanks in advance. Guillermo -- You received this message because you are subscribed to the Google Groups

[sage-support] Re: Sorting a list of variables

2019-04-21 Thread Nils Bruin
sorted(L, key=lambda v: (v[:1],int(v[1:]))) would do the trick. In general, you could look at something like https://pypi.org/project/natsort/. It might be able to make a more natural sortkey in more examples (in general, the idea would be to split your string in alphabetic and numerical substr

Re: [sage-support] Re: Sorting a list of variables

2019-04-21 Thread G. M.-S.
Thank you very much, Nils. As I have a list of variables, I changed it to sorted(L, key=lambda v: (str(v)[:1],int(str(v)[1:]))) Another question: How can I get natsort? from natsort import natsorted gives ImportError: No module named natsort On Sun, 21 Apr 2019 at 23:59, Nils Bruin wrote: >