如何使用 python map() 函数来处理元组列表?
How can I use the python map() function to handle a list of tuples?
我目前有两个列表和一个工作脚本,当给定 list_a
中的元素时,它会找到 list_b
中 30 分钟内的所有日期时间。社区在 .
中帮助了我
我用元组列表替换了其中一个列表,并一直试图在脚本中协调它——我将在下面解释我试图做出的一些修改。
# Old Set of Two Lists
list_a = ["10:26:42", "8:55:43", "7:34:11"]
list_b = ["10:49:20", "8:51:10", "10:34:35", "8:39:47", "7:11:49", "7:42:10"]
# Previous Output
10:26:42 is within 30m of 10:49:20, 10:34:35
08:55:43 is within 30m of 08:51:10, 08:39:47
07:34:11 is within 30m of 07:11:49, 07:42:10
# Old List and New List of Tuples
my_flights = ["10:26:42", "8:55:43", "7:34:11"]
alt_flights = [("10:49:20", "Frontier"), ("8:51:10", "Southwest"), ("10:34:35", "Jet Blue"), ("8:39:47", "Delta"), ("7:11:49", "Spirit"), ("7:42:10", "American"]
# Desired Output
10:26:42 is within 30m of Frontier, Jet Blue at 10:49:20, 10:34:35
10:26:42 is within 30m of Southwest, Delta at 08:51:10, 08:39:47
10:26:42 is within 30m of Spirit, American at 07:11:49, 07:42:10
旧的两个列表的工作脚本如下,我要做的是 (1) 将 list_a
替换为 my_flights
(2) 替换 list_b
使用 alt_flights
和 (3) 在我的输出中使用 alt_flights
中的名称!
def str2time(s):
h,m,s = map(int, s.split(':'))
return datetime.timedelta(hours=h, minutes=m, seconds
z = datetime.datetime(1900,1,1)
for a in map(str2time, list_a):
start = f'{z+a:%H:%M:%S} is within 30m of'
for b in map(str2time, list_b):
if abs(a-b).total_seconds() <= 1800:
print(f'{start} {z+b:%H:%M:%S}', end='')
start = ','
if start == ',':
print()
尝试修改:
我想我可以将 list_b
更改为 for b in map(str2time, alt_flights[0][0])
并稍后引用 alt_flights[0][1]
以提取航空公司名称,但这会引发 str2time()
的错误。然后我想我可以在 if abs(a-b).total_seconds() <= 1800:
下面做一个检查,检查 if b in alt_flights
会获取当前航空公司名称值 list_b[0][1]
。但是,这两个都失败了。
您有什么特殊原因需要使用map
?
for b_time_str, b_name in alt_flights:
b_time = str2time(b_time_str)
if abs(a-b_time).total_seconds() <= 1800:
print(f"{start} {z+b_time:%H:%M:%S} {b_name}")
与其在循环之前映射 alt_flights
中的时间,不如在打印期间将时间数据传递给 str2time
可能更易于阅读。如果你更换
for b in map(str2time, list_b):
if abs(a-b).total_seconds() <= 1800:
print(f'{start} {z+b:%H:%M:%S}', end='')
和
for time, airline in alt_flights:
if abs(a - str2time(time)).total_seconds() <= 1800:
print(f"{start} {str2time(time)} {airline}", end='')
代码将按预期运行。
完整的第二次迭代:
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()
输出:
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
我目前有两个列表和一个工作脚本,当给定 list_a
中的元素时,它会找到 list_b
中 30 分钟内的所有日期时间。社区在
我用元组列表替换了其中一个列表,并一直试图在脚本中协调它——我将在下面解释我试图做出的一些修改。
# Old Set of Two Lists
list_a = ["10:26:42", "8:55:43", "7:34:11"]
list_b = ["10:49:20", "8:51:10", "10:34:35", "8:39:47", "7:11:49", "7:42:10"]
# Previous Output
10:26:42 is within 30m of 10:49:20, 10:34:35
08:55:43 is within 30m of 08:51:10, 08:39:47
07:34:11 is within 30m of 07:11:49, 07:42:10
# Old List and New List of Tuples
my_flights = ["10:26:42", "8:55:43", "7:34:11"]
alt_flights = [("10:49:20", "Frontier"), ("8:51:10", "Southwest"), ("10:34:35", "Jet Blue"), ("8:39:47", "Delta"), ("7:11:49", "Spirit"), ("7:42:10", "American"]
# Desired Output
10:26:42 is within 30m of Frontier, Jet Blue at 10:49:20, 10:34:35
10:26:42 is within 30m of Southwest, Delta at 08:51:10, 08:39:47
10:26:42 is within 30m of Spirit, American at 07:11:49, 07:42:10
旧的两个列表的工作脚本如下,我要做的是 (1) 将 list_a
替换为 my_flights
(2) 替换 list_b
使用 alt_flights
和 (3) 在我的输出中使用 alt_flights
中的名称!
def str2time(s):
h,m,s = map(int, s.split(':'))
return datetime.timedelta(hours=h, minutes=m, seconds
z = datetime.datetime(1900,1,1)
for a in map(str2time, list_a):
start = f'{z+a:%H:%M:%S} is within 30m of'
for b in map(str2time, list_b):
if abs(a-b).total_seconds() <= 1800:
print(f'{start} {z+b:%H:%M:%S}', end='')
start = ','
if start == ',':
print()
尝试修改:
我想我可以将 list_b
更改为 for b in map(str2time, alt_flights[0][0])
并稍后引用 alt_flights[0][1]
以提取航空公司名称,但这会引发 str2time()
的错误。然后我想我可以在 if abs(a-b).total_seconds() <= 1800:
下面做一个检查,检查 if b in alt_flights
会获取当前航空公司名称值 list_b[0][1]
。但是,这两个都失败了。
您有什么特殊原因需要使用map
?
for b_time_str, b_name in alt_flights:
b_time = str2time(b_time_str)
if abs(a-b_time).total_seconds() <= 1800:
print(f"{start} {z+b_time:%H:%M:%S} {b_name}")
与其在循环之前映射 alt_flights
中的时间,不如在打印期间将时间数据传递给 str2time
可能更易于阅读。如果你更换
for b in map(str2time, list_b):
if abs(a-b).total_seconds() <= 1800:
print(f'{start} {z+b:%H:%M:%S}', end='')
和
for time, airline in alt_flights:
if abs(a - str2time(time)).total_seconds() <= 1800:
print(f"{start} {str2time(time)} {airline}", end='')
代码将按预期运行。
完整的第二次迭代:
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()
输出:
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