you can user sorted..reverse as per doc
def main():
print('Celebrities known by one name:')
drake = ['Drake']
BE = ['Beyonce']
RI = ['Rihanna']
BO = ['Bono']
a = input('Enter another one name celebrity ')
b = input('Enter another one name celebrity ')
c = input('Enter another one name celebrity ')
d = [a,b,c,drake,BE,RI,BO]
f = sorted(d,reverse=True)
print('Celebrities in Reverse Alpha Order')
for d in f:
print(d)
main()
Answer from Dharmesh Fumakiya on Stack OverflowHow can you use the sorted() function to print a list in reverse alphabetical order? (Python) - Stack Overflow
Python reverse alphabetical order - Stack Overflow
python - How to sort a list by length and then in reverse alphabetical order - Stack Overflow
python - Sort a list in reverse order - Stack Overflow
you can user sorted..reverse as per doc
def main():
print('Celebrities known by one name:')
drake = ['Drake']
BE = ['Beyonce']
RI = ['Rihanna']
BO = ['Bono']
a = input('Enter another one name celebrity ')
b = input('Enter another one name celebrity ')
c = input('Enter another one name celebrity ')
d = [a,b,c,drake,BE,RI,BO]
f = sorted(d,reverse=True)
print('Celebrities in Reverse Alpha Order')
for d in f:
print(d)
main()
You can try this:
l = ['Bono', 'Rihanna', 'Beyonce', 'Drake', 'eminem', 'adele', 'pink']
import string
final_list = sorted(l, key=lambda x:26 - string.ascii_lowercase.index(x[0].lower()))
Output:
['Rihanna', 'pink', 'eminem', 'Drake', 'Bono', 'Beyonce', 'adele']
You can use list.sort with the reverse argument:
>>> l = [(3, 'one'), (2, 'was'), (2, 'two'), (1, 'too'), (1, 'racehorse'), (1, 'a')]
>>> l.sort(key=lambda x: x[1], reverse=True)
>>> l.sort(key=lambda x: x[0])
>>> l
[(1, 'too'), (1, 'racehorse'), (1, 'a'), (2, 'was'), (2, 'two'), (3, 'one')]
Define the list:
>>> mylist = [(3, 'one'), (2, 'was'), (2, 'two'), (1, 'too'), (1, 'racehorse'), (1, 'a')]
Sort the list:
>>> sorted(mylist, key=lambda x: (-x[0], x[1]), reverse=True)
[(1, 'too'), (1, 'racehorse'), (1, 'a'), (2, 'was'), (2, 'two'), (3, 'one')]
Just use the reversed function:
a = list(reversed(sorted(a, key=lambda x: (-len(x), x))))
In [301]: a
Out[301]: ['b', 'a', 'zzz', 'ddd', 'ccc']
Here is another approach useing cmp_to_key() from functools:
import functools
def customsort(a, b):
if len(a) != len(b):
return -1 if len(a) < len(b) else 1
else:
if a < b:
return 1
elif a > b:
return -1
else:
return 0
def main():
a=['a','b','zzz','ccc','ddd']
a.sort(key=functools.cmp_to_key(customsort))
print(a)
main()
Output:
['b', 'a', 'zzz', 'ddd', 'ccc']
Your mistake is using sorted, which rearranges the list in order of the elements and ignores where the elements used to be. Instead use
b = a[::-1]
That runs through list a in reverse order. You also could use
b = list(reversed(a))
although the first version is faster.
If you want to use sorted(), you can specify that the index is the key to sort on:
b = sorted(a, key=a.index, reverse=True)
Sample output with input: 'Jan Sam Ann Joe Tod'
['Tod', 'Sam', 'Joe', 'Jan', 'Ann']
My code:
user_input = input() short_names = user_input.split() short_names = ['Jan', 'Sam', 'Ann', 'Joe', 'Tod'] short_names.sort(reverse=True) print(short_names)
How do I edit the code to allow for any user input? For example I want to test with input 'Lew Sue Moe Al Jay'?
Thanks!