[Baypiggies] sorting alphanumeric list

Aahz aahz at pythoncraft.com
Sun Jul 24 22:51:07 CEST 2011


On Tue, Jul 19, 2011, Vikram K wrote:
>
> given the following list:
> 
> >>> a
> ['Y6', 'T5']
> 
> how do i modify list a to end up with:
> 
> >>> b
> ['T5', 'Y6']

>>> a = ['Y6', 'T5']
>>> b = sorted(a)
>>> b
['T5', 'Y6']

However, I must point out that you have a "wrong question" situation.
What you are demonstrating in your code is *not* a modification of a.
Answering your question literally leads to this:

>>> a = ['Y6', 'T5']
>>> a.sort()
>>> b = a
>>> b
['T5', 'Y6']
>>> a is b
True
>>> a.append('Z3')
>>> b
['T5', 'Y6', 'Z3']

Programming depends very much on precise statement of problem and
solution.

You should read this:
http://starship.python.net/crew/mwh/hacks/objectthink.html
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"If you don't know what your program is supposed to do, you'd better not
start writing it."  --Dijkstra


More information about the Baypiggies mailing list