creating an artificial "last element" in sort list

D

dave

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

Ian Kelly

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.
 
D

dave

more clearer, this is a more realistic use case:

['awefawef', 'awefawfsf', 'awefsdf', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz']

and the quantity of ''zzzzzzzzzzzzzz'' would be dynamic.

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.
 
D

dave

more clearer, this is a more realistic use case:

['awefawef', 'awefawfsf', 'awefsdf', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz']

and the quantity of ''zzzzzzzzzzzzzz'' would be dynamic.

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.
 
8

88888 Dihedral

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.



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.
 
8

88888 Dihedral

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.



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.
 
D

Demian Brecht

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.
 
D

Demian Brecht

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.
 
D

Demian Brecht

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.
 
D

duncan smith

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
 
P

Paul Rubin

dave said:
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.
 
S

Steven D'Aprano

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.
 
I

Ian Kelly

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top