How can I use the python map() function to handle a list of tuples?
Instead of mapping the times in alt_flights
before the loop, it might be easier to read if you pass the time data to str2time
during printing. If you replace
for b in map(str2time, list_b):
if abs(a-b).total_seconds() <= 1800:
print(f'{start} {z+b:%H:%M:%S}', end='')
with
for time, airline in alt_flights:
if abs(a - str2time(time)).total_seconds() <= 1800:
print(f"{start} {str2time(time)} {airline}", end='')
the code will work as expected.
The full second iteration:
for a in map(str2time, my_flights):
start = f'{z+a:%H:%M:%S} is within 30m of'
for time, airline in alt_flights:
if abs(a - str2time(time)).total_seconds() <= 1800:
print(f"{start} {str2time(time)} {airline}", end='')
start = ','
if start == ',':
print()
Output:
10:26:42 is within 30m of 10:49:20 Frontier, 10:34:35 Jet Blue
08:55:43 is within 30m of 8:51:10 Southwest, 8:39:47 Delta
07:34:11 is within 30m of 7:11:49 Spirit, 7:42:10 American