• Register
0 votes
113 views

Problem :

I am unable to understand what am I missing here:

Sort my_short_names in reverse alphabetic order. The sample output from the given program as below :

['Tody', 'Samy', 'Joey', 'Jany', 'Anny']

Below is my code for your reference :

my_short_names = ['Jany', 'Samy', 'Anny', 'Joey', 'Tody']
my_short_names.sort()
print(my_short_names)

 

6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution  :

I had also faced the similar issue in the recent past. I did lot of research on it and found the solution on it. This is the very common problem with the people getting started.

As I always say, first please have the very careful look at the documentation for list.sort:

sort(*, key=None, reverse=None)

 This method sorts your list in a place, using only the < comparisons between items.

The reverse is the boolean value. If it is set to True, then your list elements are sorted as if each comparison were to be reversed.

So the items in the list will be sorted from the "smallest" to the "largest" using your < comparion operator, which for strings clearly means the lexicographical ordering (A < AB < B). So to start the sort in reverse order, you need to use the reverse parameter as shown below :

my_short_names.sort(reverse=True)

For more information on it please have a look at the official information Sorting HOW TO.

 

9 7 4
38,600 points

Related questions

0 votes
1 answer 851 views
851 views
Problem : I learning Python with the help of free online Python tutorial which wants me to write following Program: Create a temperature converter which will convert Fahrenheit values to Celsius and vice-versa using the following two formulas which relate the temperature f in Fahrenheit to ... ,sep ='') if inputString[-1] == 'F': farenheit = inputDigit print(celsiusConvert(farenheit),'C', sep='')
asked Nov 21, 2019 peterlaw 6.9k points
0 votes
1 answer 1.4K views
1.4K views
Problem : I am trying to convert my working Python 2.7 code into Python 3 code but I am facing following error post data should be bytes, an iterable of bytes, or a file object. it cannot be of type str.
asked Nov 16, 2019 peterlaw 6.9k points
0 votes
1 answer 16 views
16 views
Problem: typeerror: '_io.textiowrapper' object is not subscriptable.
asked Feb 19 Dan phillip 1.9k points
0 votes
1 answer 17 views
17 views
Problem: error: (-215:assertion failed) !empty() in function 'cv::cascadeclassifier::detectmultiscale'
asked Feb 19 Lucky Guy 1.4k points
0 votes
1 answer 9 views
9 views
Problem input contains nan, infinity or a value too large for dtype('float64').
asked Feb 11 charles mathews 3.8k points
0 votes
1 answer 740 views
740 views
Problem: I have recently started learning Python so I have only fundamental knowledge in Python. Please find below my Transaction class for your reference: class Transaction(object):     def __init__(self, company, num, price, date, is_buy):         self.company = company         ... Python and because of lockdown I am unable to find any help to fix this issue. So I am posting my question here.
asked Aug 10, 2020 Raphael Pacheco 4.9k points
1 vote
1 answer 185 views
185 views
Problem: I am getting an interesting error: valueerror: mixing iteration and read methods would lose data while I&rsquo;m trying to run my script on python. I have no idea about this problem. I am trying to open a text file in my program and read it. Please ... test_file I think I am doing something wrong with using the readline() function. I am looking forward to having a solution. Thanks, guys.
asked Mar 29, 2020 Gavin 15.3k points
0 votes
1 answer 554 views
554 views
Problem : I am trying to execute below commands. import numpy as np import cv But I am getting following error on my console: >>> runfile('/Users/isaiahnields/.spyder2/temp.py', wdir='/Users/isaiahnields/.spyder2') RuntimeError: module compiled against API version a but this ... it is not working. I had added cv.py to the python2.7 folder in Spyder-Py2 is there anything else I need to add?
asked Dec 4, 2019 alecxe 7.5k points
0 votes
2 answers 856 views
856 views
Problem : On installing Anaconda on Ubantu getting below Python error on miss-typed commands fatal python error: py_initialize: unable to get the locale encoding
asked Oct 21, 2019 peterlaw 6.9k points
1 vote
2 answers 4.2K views
4.2K views
Problem : Getting bellow python and django error 'virtualenv' is not recognized as an internal or external command, operable program or batch file.
asked Nov 8, 2019 peterlaw 6.9k points