简体   繁体   English

具有比较条件的列表理解内的列表理解

[英]List comprehension inside a list comprehension with comparison condition

I have a list like this,我有一个这样的列表,

 sl=[[1,2,100],[2,100,4],[100,4,5],[4,5,6],[5,6,200],[6,200,7],[200,7,8],[7,8,300],[8,300,9]]

Now I want to to find those element where the element is greater than the mean and store it into a list.现在我想找到那些元素大于平均值的元素并将其存储到列表中。

so, the list will look like,所以,列表看起来像,

  [[100],[100],[100],[],[200],[200],[200],[300]]

I can do it using for loop, the code is following,我可以使用 for 循环来完成,代码如下,

indices=[]
for i in sl:
    indices.append([j for j in i if (j>(np.mean(i))])

But the execution time is long to execute.但是执行时间长执行。 I want to avid the for loop and use some kind of list comprehension to do the same task.我想使用 for 循环并使用某种列表理解来完成相同的任务。

Is there any way to do it most efficient execution time?有什么办法可以做到最有效的执行时间?

You should calculate the mean only once per loop.您应该在每个循环中只计算一次平均值。 This should improve things a bit:这应该会有所改善:

indices = []
for i in sl:
  mean = np.mean(i)
  indices.append([j for j in i if j > mean])

I don't think converting the whole thing to a nested list comprehension would help, because we want to extract the mean calculation outside of the innermost loop.我不认为将整个事情转换为嵌套列表理解会有帮助,因为我们想在最内层循环之外提取均值计算。

For a regular shaped list, ie same number of elements per list at the inner nested level, we can use NumPy tools to offload the compute part, like so -对于规则形状的列表,即内部嵌套级别每个列表的元素数量相同,我们可以使用 NumPy 工具来卸载计算部分,就像这样 -

a = np.array(sl)
m = a>a.mean(1,keepdims=True)
idx = np.r_[0,m.sum(1).cumsum()]
f = a[m].tolist()
out = [f[i:j] for (i,j)in zip(idx[:-1],idx[1:])]

This would be beneficial for a large number of entries in sl , ie for a large len(sl) .这将有利于sl中的大量条目,即大len(sl)

you can have everything in one list comprehension avoiding for loop, getting a bit of performance:您可以将所有内容都放在一个列表理解中,避免循环,获得一些性能:

means = map(np.mean, sl)
indices = [list(filter(lambda x: x > m, l)) for l, m in zip(sl, means)]

You can also do:你也可以这样做:

sl=[[1,2,100],[2,100,4],[100,4,5],[4,5,6],[5,6,200],[6,200,7],[200,7,8],[7,8,300],[8,300,9]]

sl = np.array(sl)

# List of elements satisfying the condition
elem_list = [sl[i][m].tolist() for i,m in enumerate((sl.T>sl.mean(axis=1)).T)]

# List of index for which the respective elements satisfy the condition
index_list = [np.where(m)[0].tolist() for m in (sl.T>sl.mean(axis=1)).T]

Which results in:结果是:

elem_list
[[100], [100], [100], [6], [200], [200], [200], [300], [300]]

index_list
[[2], [1], [0], [2], [2], [1], [0], [2], [1]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM