Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30432 > unrolled thread

creating an artificial "last element" in sort list

Started bydave <davidreynon@gmail.com>
First post2012-09-28 16:39 -0700
Last post2012-09-29 02:05 +0000
Articles 13 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  creating an artificial "last element" in sort list dave <davidreynon@gmail.com> - 2012-09-28 16:39 -0700
    Re: creating an artificial "last element" in sort list Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-28 17:45 -0600
      Re: creating an artificial "last element" in sort list dave <davidreynon@gmail.com> - 2012-09-28 16:51 -0700
        Re: creating an artificial "last element" in sort list 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-28 17:24 -0700
        Re: creating an artificial "last element" in sort list 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-28 17:24 -0700
        Re: creating an artificial "last element" in sort list Demian Brecht <demianbrecht@gmail.com> - 2012-09-28 17:32 -0700
          Re: creating an artificial "last element" in sort list Demian Brecht <demianbrecht@gmail.com> - 2012-09-28 17:59 -0700
            Re: creating an artificial "last element" in sort list Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-28 21:29 -0600
          Re: creating an artificial "last element" in sort list Demian Brecht <demianbrecht@gmail.com> - 2012-09-28 17:59 -0700
        Re: creating an artificial "last element" in sort list duncan smith <buzzard@invalid.invalid> - 2012-09-29 02:19 +0100
      Re: creating an artificial "last element" in sort list dave <davidreynon@gmail.com> - 2012-09-28 16:51 -0700
    Re: creating an artificial "last element" in sort list Paul Rubin <no.email@nospam.invalid> - 2012-09-28 18:42 -0700
    Re: creating an artificial "last element" in sort list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-29 02:05 +0000

#30432 — creating an artificial "last element" in sort list

Fromdave <davidreynon@gmail.com>
Date2012-09-28 16:39 -0700
Subjectcreating an artificial "last element" in sort list
Message-ID<7cd170ac-bb14-4baa-93ea-530c569d3985@googlegroups.com>
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!!!

[toc] | [next] | [standalone]


#30433

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-09-28 17:45 -0600
Message-ID<mailman.1587.1348875975.27098.python-list@python.org>
In reply to#30432
On Fri, Sep 28, 2012 at 5:39 PM, dave <davidreynon@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#30434

Fromdave <davidreynon@gmail.com>
Date2012-09-28 16:51 -0700
Message-ID<b3564f31-f54c-4c00-a6e7-f497911c054a@googlegroups.com>
In reply to#30433
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.

[toc] | [prev] | [next] | [standalone]


#30437

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-09-28 17:24 -0700
Message-ID<6646e40a-bb2a-468b-87ab-767b2e6b42d0@googlegroups.com>
In reply to#30434
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. 

[toc] | [prev] | [next] | [standalone]


#30438

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-09-28 17:24 -0700
Message-ID<mailman.1590.1348878303.27098.python-list@python.org>
In reply to#30434
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. 

[toc] | [prev] | [next] | [standalone]


#30440

FromDemian Brecht <demianbrecht@gmail.com>
Date2012-09-28 17:32 -0700
Message-ID<mailman.1591.1348878745.27098.python-list@python.org>
In reply to#30434
Apparently gmail hates me and my last response didn't get through:

a = ['awefawef', 'awefawfsf', 'awefsdf', 'zzzzzzzzzzzzzz', 
'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz']
f = filter(lambda s: s == a[-1], a)
l = sorted(lst[:-len(f)]) + f

Now, not 100% sure about efficiency over large sizes of a, but that's a 
naive stab at it anyway.

-- 
Demian Brecht
@demianbrecht
http://demianbrecht.github.com

[toc] | [prev] | [next] | [standalone]


#30444

FromDemian Brecht <demianbrecht@gmail.com>
Date2012-09-28 17:59 -0700
Message-ID<16506499-9803-4f68-8269-f201863574c9@googlegroups.com>
In reply to#30440
> f = filter(lambda s: s == a[-1], a)

That line's assuming that the last element may also be found in arbitrary locations 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.

[toc] | [prev] | [next] | [standalone]


#30462

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-09-28 21:29 -0600
Message-ID<mailman.1601.1348889426.27098.python-list@python.org>
In reply to#30444
On Fri, Sep 28, 2012 at 6:59 PM, Demian Brecht <demianbrecht@gmail.com> wrote:
>> f = filter(lambda s: s == a[-1], a)
>
> That line's assuming that the last element may also be found in arbitrary locations 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.

[toc] | [prev] | [next] | [standalone]


#30445

FromDemian Brecht <demianbrecht@gmail.com>
Date2012-09-28 17:59 -0700
Message-ID<mailman.1595.1348880397.27098.python-list@python.org>
In reply to#30440
> f = filter(lambda s: s == a[-1], a)

That line's assuming that the last element may also be found in arbitrary locations 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.

[toc] | [prev] | [next] | [standalone]


#30449

Fromduncan smith <buzzard@invalid.invalid>
Date2012-09-29 02:19 +0100
Message-ID<50664122$0$7414$a8266bb1@newsreader.readnews.com>
In reply to#30434
On 29/09/12 00:51, dave wrote:
> more clearer, this is a more realistic use case:
>
> ['awefawef', 'awefawfsf', 'awefsdf', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz']
>
> and the quantity of ''zzzzzzzzzzzzzz'' would be dynamic.
>

Maybe,

class Greatest:

     def __lt__(self, other):
         return False

     def __eq__(self, other):
         return type(other) == type(self)

etc.

Duncan

[toc] | [prev] | [next] | [standalone]


#30435

Fromdave <davidreynon@gmail.com>
Date2012-09-28 16:51 -0700
Message-ID<mailman.1588.1348876273.27098.python-list@python.org>
In reply to#30433
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.

[toc] | [prev] | [next] | [standalone]


#30451

FromPaul Rubin <no.email@nospam.invalid>
Date2012-09-28 18:42 -0700
Message-ID<7xlift8v0c.fsf@ruckus.brouhaha.com>
In reply to#30432
dave <davidreynon@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#30454

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-09-29 02:05 +0000
Message-ID<5066575b$0$29981$c3e8da3$5496439d@news.astraweb.com>
In reply to#30432
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web