+ 4
18 Respuestas
+ 3
You could define your own string class with it's own comparison magic methods
Or just make a function and call it with the 2 strings as parameters
Or fork the python language and crate your own with this feature (but this may not be the best nor the simplest solution...)
+ 3
I'm afraid this will be a little bit too complicated to be explained (expecially to a beginner) with a Q&A answer...
Also, can you be more precise on what is the order you intend (maybe with examples)?
+ 1
Ali_combination
Are you talking about comparison of 2 strings, or about sorting an iterable? I'm not getting what you are doing here.
+ 1
Ok, so it's just a matter of comparing the lengths, and if they are equal compare the strings in the normal way, am I right?
For the <
if len(s1) == len(s2) :
return s1 < s2
else:
return len(s1) < len(s2)
Or you can make it return -1 0 1 for <=>
+ 1
You can do anything you want, but some are more difficult than others...
Btw, here is a compare function
def compare(s1, s2):
if s1 == s2: return 0
if len(s1) == len(s2):
return 1 if s1>s2 else -1
else:
return 1 if len(s1) > len(s2) else -1
+ 1
Angelo Thank you so much , i just didnt get it.. what is len? An especial method or any function?
0
I got so many examples in ma mind , but d simplest is like this..we use just a and b , and the order is like this : a , aa , aaa , aab , ab , aba , abb , b , ba , baa , bab , bb , bba , bbb , ... and maximize length of strings are 6 . So first , we write all strings which their length is 1 , then write all strings which their length is 2 , and to the end ..
0
Angelo I think you are right.. and i should improve ma knowledge.. so i can compare it to any order i like , right?
0
len is a built-in function that returns the length of a string
0
Angelo 👍👍 thank you.
0
You are welcome Ali_combination
0
ok
0
I thing the following will help
list["z","y","x",...,"c","b","a"]
So you defind a list of your own alphabet order, now you can iterate the list and list[0] is the character z list[1] = y, list[2] = "x".....list[25] = a.
you could also reverse the order of list characters using list[::-1] so that you get the original alphabet order. in a list you can put the order that you want.
Hope you will get to the result you want, and consider revising the list module.
0
You are welcome🌼 Ali_combination