I'm a true newbie in Python. Using a learning book I've got this exercise among others. I do know how to do that with method, but not with a function sorted([, , , ] ).
I've tried different approaches to try to understand but i didn't.
So in the most simple words. How do you do it?
Thanks you
Try this,
sorted('abcdef',reverse=True)
If I want to sort the whole list
wow=['usa', 'australia', 'brazil', 'ireland']
print(sorted(wow))
print(sorted(wow, reverse=True))
Hope you got it.
I come across a similar exercise in the book Python Crash Course A Hands-On, Project-Based Introduction to Programming .
If you want to reverse the letters in a string, you could try this, just put the string inside the parentheses.
print(sorted("happyday",reverse=True))
.
outcome: ['y', 'y', 'p', 'p', 'h', 'd', 'a', 'a']
If you want to reverse the elements in a list, you could try this:
want_to_visit=["Berlin","Thailand","London","Greek","Agentina"]
print(sorted(want_to_visit,reverse=True))
outcome: ['Thailand', 'London', 'Greek', 'Berlin', 'Agentina']
This worked for me:
cities = ["London", "Paris", "Rome", "Los Angeles", "New York"]
cities.sort(reverse=True)
print(cities)
This is hilarious. I was just doing the same exercise and having the same problem. It is from the "PYTHON CRASH COURSE" chapter 3.8 to be exact.
cities = ["London", "Paris", "Rome", "Los Angeles", "New York"]
cities.sort(reverse=True)
print(cities)
works to permanently sort the list into reverse alphabetical order whereas
print (sorted(cities,reverse=True))
gives you a temporary reverse alphabetical sort of the list
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.