【问题标题】:Why does this list comprehension based on zip object does not work in subsequent loop iterations?为什么这个基于 zip 对象的列表理解在后续循环迭代中不起作用?
【发布时间】:2022-01-16 18:44:44
【问题描述】:

我有一段代码,我想在连续循环迭代中过滤掉列表的一部分:

def calculate_delays(flash_time_stamps, sample_time_stamps, sample_values, threshold):
    delays = []
    first_thresh_cross_time_stamps = []
    samples = zip(sample_time_stamps, sample_values)
    # For each flash find the first sample that crosses the chosen threshold 
    # and calculate the difference between the corresponding timestamps
    for flash_time_stamp in flash_time_stamps:
        first_cross_thresh_time_stamp = -1
        # Ignore samples that occured before the flash
        samples_filtered = [s for s in samples if s[0] >= flash_time_stamp]    # ---COMPREHENSION IS HERE---
        for sample in samples_filtered:
            if sample[1] < threshold:
                first_cross_thresh_time_stamp = sample[0]
                break

        # Save
        first_thresh_cross_time_stamps.append(first_cross_thresh_time_stamp)
        delays.append(first_cross_thresh_time_stamp - flash_time_stamp)
    
    return first_thresh_cross_time_stamps, delays

在第一次迭代中,代码按预期工作,但在随后的迭代中,列表推导返回一个空列表。根据我传递的数据,我知道这不应该是这种情况。此外,以下代码按预期工作:

def calculate_delays(flash_time_stamps, sample_time_stamps, sample_values, threshold):
    delays = []
    first_thresh_cross_time_stamps = []
    samples = zip(sample_time_stamps, sample_values)
    # For each flash find the first sample that crosses the chosen threshold 
    # and calculate the difference between the corresponding timestamps
    for flash_time_stamp in flash_time_stamps:
        first_cross_thresh_time_stamp = -1
        # Ignore samples that occured before the flash
        for sample in samples:
            if sample[0] < flash_time_stamp:    # ---CHANGE HERE---
                continue
            if sample[1] < threshold:
                first_cross_thresh_time_stamp = sample[0]
                break

        # Save
        first_thresh_cross_time_stamps.append(first_cross_thresh_time_stamp)
        delays.append(first_cross_thresh_time_stamp - flash_time_stamp)
    
    return first_thresh_cross_time_stamps, delays

我在这里做错了什么?

【问题讨论】:

    标签: python loops list-comprehension


    【解决方案1】:

    当您遍历 zip 对象时,它会弹出所有数据,因此第二次这是一个空列表 samples_filtered = [s for s in samples if s[0] &gt;= flash_time_stamp],因此您可以像这样随时随地压缩数据:

    samples_filtered = [s for s in zip(sample_time_stamps, sample_values) if s[0] >= flash_time_stamp]
    

    【讨论】:

      【解决方案2】:

      我什至不会在这里使用列表推导,第二个 sn-p 会更有效,因为如果第一个元素让您提前中断循环,您就不会不必要地创建列表。

      您可以直接使用 next 并完全替换整个 for 循环

      first_cross_thresh_time_stamp = next(
          (s[0] for s in samples if s[0] >= flash_time_stamp and s[1] < threshold),
          -1
      )
      first_thresh_cross_time_stamps.append(first_cross_thresh_time_stamp)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-28
        • 2019-09-27
        • 2017-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-11
        • 1970-01-01
        相关资源
        最近更新 更多