G
G
Guerro692020-06-16 19:16:09
Python
Guerro69, 2020-06-16 19:16:09

How to sort two numbers in a list?

There is a code where I tried to implement it myself:

hours = ['0', '3', '2', '7']
minutes = ['23', '9', '16', '10']
time = ''
for i, n in enumerate(sorted(zip(minutes, hours), key=lambda n: int(n[0:2]), reverse=True), 1):
    time += f"{i}. Время: {n[1]}ч {n[0]}м\n"
print(time)

It turns out that the error is not surprising:
TypeError: int () argument must be a string, a bytes-like object or a number, not 'tuple'

The essence is that I need to sort both hours and minutes.
Initially, I was sorted only by minutes and it was like this:
"1. Time: 0h 23m
2. Time: 2h 16m
3. Time: 7h 10m
4. Time: 3h 9m"

And you need to sort both hours and minutes, so that get this:
"1. Time: 7h 10m
2. Time: 3h 9m
3. Time: 2h 16m
4. Time: 0h 23m"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-06-16
@Guerro69

import datetime
for i, n in enumerate(sorted(zip(hours, minutes), key=lambda n: datetime.time(int(n[0]), int(n[1])), reverse=True)):
   print(f"{i}. Время: {n[0]}ч {n[1]}м")

0. Время: 7ч 10м
1. Время: 3ч 9м
2. Время: 2ч 16м
3. Время: 0ч 23м

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question