gpt4 book ai didi

python - 如何使用 lambda 表达式进行比较?

转载 作者:太空宇宙 更新时间:2023-11-03 20:17:56 27 4
gpt4 key购买 nike

我有一个硬件问题,需要遍历机场列表并使用 lambda 表达式找到最佳和最差的准点记录。

该列表包含两列“机场名称”和“延迟出发”。

因此,如果延迟出发 > 0,我们将延迟计为 1,否则我们将准时计为。我如何使用 lambda 表达式来做到这一点?

for x in data:
if airport != x.Orig:
continue
delay = list(map(lambda x: x.Orig = airport and x.DepDelay > 0, data))
print(len(delay))
ontime = list(map(lambda x: x.Orig = airport and x.DepDelay == 0, data))
print(len(ontime))

perf = ontime / (ontime +delay)

if perf < minPerf:
minAiport = airport
minPerf = perf
if perf > maxPerf:
maxAirport = airport
maxPerf = perf

我希望 lambda 表达式能够识别机场名称及其延误和准点频率。然后我可以执行 max 和 min 来找出哪些机场的最佳和最差性能。

例如,airportA 是最好的,性能:airportB 是最差的,性能:

最佳答案

根据您的代码,我假设您的输入数据如下所示:

class Data:
def __init__(self, airportname, depdelay):
self.Orig = airportname
self.DepDelay = depdelay

data = [
Data('airportname1',3),
Data('airportname2',0),
Data('airportname3',2),
Data('airportname1',1),
Data('airportname2',4),
Data('airportname3',0),
Data('airportname2',0),
Data('airportname1',0),
Data('airportname3',1),
Data('airportname4',3),
Data('airportname2',2),
Data('airportname1',0),
Data('airportname4',3),
]

请尝试以下解决方案:

result = {}
for x in data:
if x.Orig not in result:
result[x.Orig] = {'delay':0, 'ontime':0}
if x.DepDelay > 0:
result[x.Orig]['delay'] += 1
else:
result[x.Orig]['ontime'] += 1

maxperf = 0
minperf = 1
maxairport = []
minairport = []
for airport, r in result.items():
a = r['ontime']
b = r['delay']
perf = a/(a+b)
print("Airport Name: {}, ontime: {}, delay: {}, perf: {}".format(airport,a,b,perf))
if perf > maxperf:
maxperf = perf
maxairport = [airport,]
elif perf == maxperf:
maxairport.append(airport)

if perf < minperf:
minperf = perf
minairport = [airport,]
elif perf == minperf:
minairport.append(airport)

print("best performance airport: {}, performance: {}".format(maxairport, maxperf))
print("worst performance airport: {}, performance: {}".format(minairport, minperf))

基于我的输入的输出是:

Airport Name: airportname1, ontime: 2, delay: 2, perf: 0.5
Airport Name: airportname2, ontime: 2, delay: 2, perf: 0.5
Airport Name: airportname3, ontime: 1, delay: 2, perf: 0.3333333333333333
Airport Name: airportname4, ontime: 0, delay: 2, perf: 0.0
best performance airport: ['airportname1', 'airportname2'], performance: 0.5
worst performance airport: ['airportname4'], performance: 0.0

关于python - 如何使用 lambda 表达式进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58335172/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com