• Register
0 votes
70 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)

 

7.5k points

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.

 

36.1k points

Related questions

0 votes
1 answer 401 views
401 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 790 views
790 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
1 vote
1 answer 44 views
44 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 Gavin 15.2k points
0 votes
1 answer 168 views
168 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 433 views
433 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