Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > creating an artificial "last element" in sort list

Reply
Thread Tools

creating an artificial "last element" in sort list

 
 
Paul Rubin
Guest
Posts: n/a
 
      09-29-2012
dave <> writes:
> What does x need to be to always be last on an ascending sort no
> matter what 'a' and 'b' are.... within reason...


Why are you trying to do that? It sounds ugly. Just sort the list with
the a's and b's. If you absolutely have to, you could make a class with
comparison methods that put all the x's at the bottom, but look for
something cleaner.
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-29-2012
On Fri, 28 Sep 2012 16:39:33 -0700, dave 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...


How about this?

a = ['a', 'b']
b = sorted(a) + ['whatever you want']

You could also do this:

x = max(a)
a.append(x)
b = sorted(a)


> 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!!!


I think you are mistaken.

py> sorted(['a', 'b', 'zzzzzzzzzzzzzzzz'])
['a', 'b', 'zzzzzzzzzzzzzzzz']



But really, I don't understand what problem you are trying to solve.
Perhaps if you explain the purpose of this, we can suggest a solution.


--
Steven
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      09-29-2012
On Fri, Sep 28, 2012 at 6:59 PM, Demian Brecht <> wrote:
>> f = filter(lambda s: s == a[-1], a)

>
> That line's assuming that the last element may also be found in arbitrarylocations in the list. If it's guaranteed that they're all contiguous at the upper bounds, I'd just walk the list backwards until I found one that wasn't matching rather than filtering.


The slicing operation in the second line assumes that they're all
collected at the end of the list anyway.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute cjt22@bath.ac.uk Python 7 09-10-2007 11:10 AM
"Next Generation Artificial Intelligence, Artificial Mind - Part One - Basic Architecture and Cognitive Structure" tommak Java 2 07-04-2006 06:03 PM
"Next Generation Artificial Intelligence, Artificial Mind - Part One - Basic Architecture and Cognitive Structure" tommak Python 1 07-04-2006 09:34 AM
"Next Generation Artificial Intelligence, Artificial Mind - Part One - Basic Architecture and Cognitive Structure" tommak C++ 0 07-04-2006 05:27 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57