G
G
Guerro692020-06-11 00:58:50
Python
Guerro69, 2020-06-11 00:58:50

How to concatenate names with sorted numbers?

There is this code:

numbers = ['324', '1002', '6', '921']
users = ['Дмитрий', 'Анатолий', 'Василий', 'Никита']
# То есть, у Дмитрия число 324, у Анатолия 1002 и т. д.

top = '\n'.join([ f"{i + 1}. {users[i]} {b}" for i, b in enumerate(sorted(map(int, numbers), reverse=True)) ])
print(f"Топ пользователей:\n\n{top}")

As a result, as expected, I get the following:
1. Dmitry 1002
2. Anatoly 921
3. Vasily 324
4. Nikita 6

And you need to get it like this:
1. Anatoly 1002
2. Nikita 921
3. Dmitry 324
4. Vasily 6

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-06-11
@Guerro69

sortedData = sorted(zip(numbers, users), key=lambda n: int(n[0]), reverse=True)
print('\n'.join(f'{i}. {n[0]} {n[1]}' for i, n in enumerate(sortedData, 1)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question