creating an artificial "last element" in sort list

88888 Dihedral dihedral88888 at googlemail.com
Fri Sep 28 20:24:59 EDT 2012


dave於 2012年9月29日星期六UTC+8上午7時51分10秒寫道:
> more clearer, this is a more realistic use case:
> 
> 
> 
> ['awefawef', 'awefawfsf', 'awefsdf', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz']
> 
> 
> 
> and the quantity of ''zzzzzzzzzzzzzz'' would be dynamic.
> 
> 
> 
> On Friday, September 28, 2012 4:46:15 PM UTC-7, Ian wrote:
> 
> 
> 
> > 
> 
> > > a = ['a', 'b', x]
> 
> > 
> 
> > >
> 
> > 
> 
> > > b = sorted(a)
> 
> > 
> 
> > >
> 
> > 
> 
> > > What does x need to be to always be last on an ascending sort no matter what 'a' and 'b' are.... within reason... I am expecting 'a' and 'b' will be not longer than 10 char's long.... I tried making x = 'zzzzzzzzzzzzzzzz' and believe it or not, this appears FIRST on the sort!!!
> 
> > 
> 
> > 
> 
> > 
> 
> > It appears last when I run the code.
> 
> > 
> 
> > 
> 
> > 
> 
> > To answer your question, though, if you want to force x to be last,
> 
> > 
> 
> > then I suggest removing it from the list and then appending it to the
> 
> > 
> 
> > end.

I am thinking if it is helpful to preprocess an arbitrary
list first into some set of unique  ordered elements before a sort. 

Anyway lists are passed by references  to functions in python. 




More information about the Python-list mailing list