G
G
Guerro692020-06-10 21:03:58
Python
Guerro69, 2020-06-10 21:03:58

How to set numbers in descending order in a loop?

There is this code:

numbers = ['324', '1002', '6', '8', '921']

text = ''
a = 0
for i in range(len(numbers)):
    text += f"{a + 1}. {numbers[i]}\n"
    a += 1
print(f"Числа от большого к маленькому\n\n{text}")


How to make it so that the numbers are displayed in descending order:
(1. 1002
2. 921
3. 324
4. 8
5. 6)
and not as they are located in the list?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2020-06-10
@Guerro69

print('\n'.join(f'{i}. {n}' for i, n in enumerate(sorted(numbers, key=int, reverse=True), 1)))

G
galaxy, 2020-06-10
@galaxy

numbers.sort(key=lambda x: int(x), reverse=True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question