有效地检查一个列表是否充满了零

Effeciently check if a list is full of zeros

我有一个 json 数据包,每秒传入三个通道。我正在对这些通道进行过滤,但通常其中 2 个是空的(只填充了零)并且只有一个通道填充了值。问题是您可以更改在应用程序上传输数据的渠道。所以一开始通道 1 可以填充数据,但随后它可以更改为通道 2,然后我还想切换到过滤通道 2 而不是通道 1。这必须有效地发生。这些是我现在尝试过的一些解决方案

class manage:
    self.channel_number = 1
    if np.any(json[self.channel_number]):
       self.channel_number = next_channel

    filtered = filter_signal(json[self.channel_number])
 

我试过的另一种方法是

只需检查列表的最大值(因为它永远不应该为 0),然后执行与上面相同的操作

class manage:
    self.channel_number = 1
    if max(json[self.channel_number]) == 0.0:
       self.channel_number = next_channel

    filtered = filter_signal(json[self.channel_number])
 

现在检查最大值是两者中最快的。但是每秒进行一次检查似乎并不有效,因为中途切换频道并不常见

json 数据设置如下: json['data'] 是所有频道的列表,所以 获取频道 1 将是 json['data'][0] 等。 数据的大小在 200-400 左右,并且是列表格式。 因此 max 会比 np.max 快 检查通道是否为 0 或已填充的任何提示?

编辑: 列表示例: [0.025, 0.025, 0.025, 0.025, 0.025, 0.025, 0.05, 0.05, 0.05, 0.05, 0.075, 0.075, 0.075, 0.075, 0.1, 0.125, 0.125, 0.125, 0.15, 0.15, 0.15, 0.15, 0.175, 0.2, 0.175 , 0.175, 0.175, 0.175, 0.175, 0.175, 0.15, 0.125, 0.125, 0.1, 0.075, 0.075, 0.05, 0.025, 0.025, 0.0, 0.0, 0.0, 0.0, 0,0.0.0.0,5, - -0.025, 0.0, 0.0, -0.025, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.025, 0.0, 0.0, - 0.025, -0.025, 0.0, -0.025, -0.025, -0.025, -0.025, 0.0, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025 , -0.025, -0.025, -0.025, -0.05, -0.025, 0.0, -0.025, -0.025, -0.025, 0.0, -0.025, -0.05, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, -0.025, -0.025 , -0.025, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, 0.0, -0.025, -0.025, 0.0, -0.025, -0.025, -0.025, -0.025, 0.0, -0.025, - 0.025, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0 .0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, -0.025, -0.025, -0.025, -0.025, - 0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, 0.0, -0.025, 0.0, 0.025, 0.1, 0.15, 0.175, 0.075, 0.05, 0.15, 0.375, -0.05, 0.15, 0.375, -0. 0.325, -0.275, -0.2, -0.15, -0.1, -0.05, -0.025, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

示例空列表: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 , 0.0]

明确地与 0 进行比较可能比将元素视为布尔值更好。

any(x != 0 for x in json[self.channel_number])

要分析单个已解析的列表,简单地使用 any 可能是最好的。使用您的 full/empty 个示例列表进行基准测试:

 9061 ns  np.any(full)
 3468 ns  max(full)
  427 ns  any(x != 0 for x in full)
   80 ns  any(full)

 9019 ns  np.any(empty)
 3604 ns  max(empty)
14775 ns  any(x != 0 for x in empty)
 1668 ns  any(empty)

但正如我所说,也许您可​​以检查 JSON,然后只解析相关的 part/channel。一些想法的基准:

   31 ns  '5' in full_json
   29 ns  full_json == empty_json_for_comparison
   82 ns  len(full_json) == empty_json_length

   41 ns  '5' in empty_json
   50 ns  empty_json == empty_json_for_comparison
   80 ns  len(empty_json) == empty_json_length

或者您可以 zip 列表并并行检查它们,这样您就不必完全检查空列表。

基准代码(Try it online!):

from timeit import repeat

setup = '''
import numpy as np
full = [0.025, 0.025, 0.025, 0.025, 0.025, 0.025, 0.05, 0.05, 0.05, 0.05, 0.075, 0.075, 0.075, 0.075, 0.1, 0.125, 0.125, 0.125, 0.15, 0.15, 0.15, 0.15, 0.175, 0.2, 0.175, 0.175, 0.175, 0.175, 0.175, 0.175, 0.15, 0.125, 0.125, 0.1, 0.075, 0.075, 0.05, 0.025, 0.025, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.025, 0.0, 0.0, 0.0, -0.025, 0.0, 0.0, -0.025, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.025, 0.0, 0.0, -0.025, -0.025, 0.0, -0.025, -0.025, -0.025, -0.025, 0.0, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, 0.0, -0.025, -0.025, -0.025, 0.0, -0.025, -0.05, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, 0.0, -0.025, -0.025, 0.0, -0.025, -0.025, -0.025, -0.025, 0.0, -0.025, -0.025, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, 0.0, -0.025, 0.0, 0.025, 0.1, 0.15, 0.175, 0.075, 0.05, 0.15, 0.35, -0.375, -0.325, -0.275, -0.2, -0.15, -0.1, -0.05, -0.025, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
empty = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,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1, 0.125, 0.125, 0.125, 0.15, 0.15, 0.15, 0.15, 0.175, 0.2, 0.175, 0.175, 0.175, 0.175, 0.175, 0.175, 0.15, 0.125, 0.125, 0.1, 0.075, 0.075, 0.05, 0.025, 0.025, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.025, 0.0, 0.0, 0.0, -0.025, 0.0, 0.0, -0.025, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.025, 0.0, 0.0, -0.025, -0.025, 0.0, -0.025, -0.025, -0.025, -0.025, 0.0, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, 0.0, -0.025, -0.025, -0.025, 0.0, -0.025, -0.05, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, 0.0, -0.025, -0.025, 0.0, -0.025, -0.025, -0.025, -0.025, 0.0, -0.025, -0.025, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.025, -0.025, -0.025, -0.025, -0.05, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, -0.025, 0.0, -0.025, 0.0, 0.025, 0.1, 0.15, 0.175, 0.075, 0.05, 0.15, 0.35, -0.375, -0.325, -0.275, -0.2, -0.15, -0.1, -0.05, -0.025, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]'
empty_json = '[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0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{xs}_json",
    "{xs}_json == empty_json_for_comparison",
    "len({xs}_json) == empty_json_length",
]

for xs in 'full', 'empty':
    for _ in range(3):
        for code in codes:
            code = code.format(xs=xs)
            number = 1000
            t = min(repeat(code, setup, number=number)) / number
            print('%5d ns ' % (t * 1e9), code)
        print()