[Tutor] improve the code
d at davea.name
Wed Nov 2 17:58:02 CET 2011
On 11/02/2011 11:54 AM, lina wrote:
> Regard the sorted(),
> I still have a question,
> how to sort something like
> ['1A', '10B', '2C', '3D']
> ['10B', '1A', '2C', '3D']
> as [ '1A', '2C', '3D','10B']
> mainly based on their digital value.
Peter answered essentially that question, in his message on this thread,
yesterday with timestamp 12:14
Essence of the answer is you can supply a key=myfunc argument to
sorted(). Then it's up to you what you put in that function. It sounds
like you want to convert any leading digits to an int, and return that int.
Once you have that function, you just use it as a keyword argument to
sorted(), something like:
something = sorted(mylist, key=myfunc)
Why don't you have a go at trying to write such a function? The exact
details depend on what you're trying to sort, a list of strings, or a
list of tuples whose two values are strings. (or a iterable
representing one of those).
More information about the Tutor