+ 4

Can we change an especial type of order in Python?

Guys I have a question about Python , when comparing strings like "a" and "aa" , can I change order of lexicography in Python ?? Like I'd rather change it to my own default... for example the first letter starts with "z" , so "z" is smaller than "a"

7th Feb 2021, 5:21 AM
Ali_combination
Ali_combination - avatar
18 ответов
+ 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...)
7th Feb 2021, 5:37 AM
Angelo
Angelo - avatar
+ 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)?
7th Feb 2021, 6:27 AM
Angelo
Angelo - avatar
+ 3
Ipang order of strings matter , as defalut (in python) a is less than b because a is the first letter and b is not..
7th Feb 2021, 6:55 AM
Ali_combination
Ali_combination - avatar
+ 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.
7th Feb 2021, 6:53 AM
Ipang
+ 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 <=>
7th Feb 2021, 6:53 AM
Angelo
Angelo - avatar
+ 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
7th Feb 2021, 7:04 AM
Angelo
Angelo - avatar
+ 1
Angelo Thank you so much , i just didnt get it.. what is len? An especial method or any function?
7th Feb 2021, 7:06 AM
Ali_combination
Ali_combination - avatar
+ 1
ihab 🌼 wow😂 i think there's a lot left to improve ma knowledge about Python so i can do anything else and code what I need then... thank you so much gentleman ! I guess that should be the best method to create my ideal program..
8th Feb 2021, 8:17 PM
Ali_combination
Ali_combination - avatar
0
Angelo of course im still a beginner in Python but , how can I define a new order ?
7th Feb 2021, 5:59 AM
Ali_combination
Ali_combination - avatar
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 ..
7th Feb 2021, 6:42 AM
Ali_combination
Ali_combination - avatar
0
Angelo I think you are right.. and i should improve ma knowledge.. so i can compare it to any order i like , right?
7th Feb 2021, 7:01 AM
Ali_combination
Ali_combination - avatar
0
len is a built-in function that returns the length of a string
7th Feb 2021, 7:06 AM
Angelo
Angelo - avatar
0
Angelo 👍👍 thank you.
7th Feb 2021, 7:08 AM
Ali_combination
Ali_combination - avatar
0
You are welcome Ali_combination
7th Feb 2021, 7:08 AM
Angelo
Angelo - avatar
0
ok
8th Feb 2021, 5:32 PM
Noobie
Noobie - avatar
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.
8th Feb 2021, 8:08 PM
ihab 🌼
ihab 🌼 - avatar
0
You are welcome🌼 Ali_combination
8th Feb 2021, 8:26 PM
ihab 🌼
ihab 🌼 - avatar