+ 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"]
6 Réponses
+ 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))
+ 5
I don't understand the difference you make between 'to order' and 'to sort' ? Sorry 😯.
+ 4
Ok, no problem Vicky.
Try this :
https://code.sololearn.com/cvL6Zt6hir7I/?ref=app
+ 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? >_<
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)
0
But actually I don’t really understand why I don’t need to define the argument “s” and the functions still can work...