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

标签 python python-3.x list oop lambda

我有一个硬件问题,需要遍历机场列表并使用 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/

相关文章:

python - 无效的 token 和无效的语法

c# - 如何解决这个 :Struct In Collection not changed

python - List into a tuple - 修改列表反射(reflect)在元组中但清空列表不会改变元组

python - 在 PyMongo 中持久化后如何获取对象的 ID?

python - 尝试 sqlite upsert 时如何纠正语法错误?

python - 导入模块导致 TypeError : module. __init__() 最多接受 2 个参数(给定 3 个)

python - 无法在 macOS 上的 Anaconda3 python3.6 上安装 OpenCV3

python - 如何制作列表列表中元素的有序频率列表

python - 从文件创建列表字典

python - 用字典中的相应值替换字符串中的键