creating an artificial "last element" in sort list

Discussion in 'Python' started by dave, Sep 29, 2012.

  1. dave

    dave Guest

    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!!!
     
    dave, Sep 29, 2012
    #1
    1. Advertising

  2. dave

    Ian Kelly Guest

    On Fri, Sep 28, 2012 at 5:39 PM, 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... I am expecting 'a' and 'b' will benot 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.
     
    Ian Kelly, Sep 29, 2012
    #2
    1. Advertising

  3. dave

    dave Guest

    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 matterwhat '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.
     
    dave, Sep 29, 2012
    #3
  4. dave

    dave Guest

    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 matterwhat '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.
     
    dave, Sep 29, 2012
    #4
  5. 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.
     
    88888 Dihedral, Sep 29, 2012
    #5
  6. 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.
     
    88888 Dihedral, Sep 29, 2012
    #6
  7. 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
     
    Demian Brecht, Sep 29, 2012
    #7
  8. > 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 theupper bounds, I'd just walk the list backwards until I found one that wasn't matching rather than filtering.
     
    Demian Brecht, Sep 29, 2012
    #8
  9. > 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 theupper bounds, I'd just walk the list backwards until I found one that wasn't matching rather than filtering.
     
    Demian Brecht, Sep 29, 2012
    #9
  10. dave

    duncan smith Guest

    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
     
    duncan smith, Sep 29, 2012
    #10
  11. dave

    Paul Rubin Guest

    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.
     
    Paul Rubin, Sep 29, 2012
    #11
  12. 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
     
    Steven D'Aprano, Sep 29, 2012
    #12
  13. dave

    Ian Kelly Guest

    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.
     
    Ian Kelly, Sep 29, 2012
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page