+ 1

Reverse the strings in a list and sort them

How can I use the following functions to reverse the strings in a list and then sort in alphabetical order? I have some problems in the following functions, and I’m grateful for any help. def my_sort(lst): return sorted(lst, key=reverse_str_sort) def reverse_str_sort(s): #type(s) is a string return reverse_str Here is the sample: #input: lst=["apple","btc","python"] #output: ["btc","apple","python"]

27th Mar 2018, 2:15 AM
Vicky Kuo
Vicky Kuo - avatar
6 ответов
+ 4
def sort(lst): return sorted(lst) def reverse_sort(lst): #type(s) is a string return sorted(lst, reverse=True) #input: lst=["apple","ztc","python"] print (lst) #sorted: print (sort (lst)) #reverse: print (reverse_sort (lst))
27th Mar 2018, 5:43 AM
Geo
Geo - avatar
+ 5
I don't understand the difference you make between 'to order' and 'to sort' ? Sorry 😯.
28th Mar 2018, 6:06 AM
Geo
Geo - avatar
+ 4
Ok, no problem Vicky. Try this : https://code.sololearn.com/cvL6Zt6hir7I/?ref=app
28th Mar 2018, 4:20 PM
Geo
Geo - avatar
+ 1
Oww.. sorry! I typed totally wrong and made you so confused. :( I meant the output has to be in alphabetical order after being reversed the strings. For example, input a list: lst=[hi, app, candy] Reverse the strings and become: [ih, ppa, ydnac] So final output should be: [hi, app, candy]. Does this make sense? >_<
28th Mar 2018, 9:29 AM
Vicky Kuo
Vicky Kuo - avatar
0
@Geo thank you for your generosity!! :) I referred to your code and found another way to solve it :) Here is my code after revising yours: def reverse_str_sort(s): return s[::-1] def my_sort(lst): return sorted(lst, key=reverse_str_sort) lst=["apple","btc","python"] my_sort(lst)
30th Mar 2018, 8:43 AM
Vicky Kuo
Vicky Kuo - avatar
0
But actually I don’t really understand why I don’t need to define the argument “s” and the functions still can work...
30th Mar 2018, 9:21 AM
Vicky Kuo
Vicky Kuo - avatar