简体   繁体   English

此特定列表理解的 Numpy 版本

[英]Numpy version of this particular list comprehension

So a few days ago I needed a particular list comprehension in this thread: Selecting a subset of integers given two lists of end points所以几天前,我需要在这个线程中进行一个特定的列表理解: 选择整数的子集给定两个端点列表

and I got a satisfying answer.我得到了满意的答复。 Fast forward now, I somehow need to boost the performance, as what I am working with involves looping it and in each iterations the lengths of these end-point arrays are at least few thousands.现在快进,我需要以某种方式提高性能,因为我正在处理的工作涉及循环它,并且在每次迭代中,这些端点数组的长度至少为几千。

So my question is is there any functions in numpy package that can get the job done but a lot faster?所以我的问题是 numpy 包中是否有任何功能可以完成工作但速度要快得多? I've looked into numpy's linspace, repeat, arange and stuff, and couldn't find any breakthrough.我查看了 numpy 的 linspace、repeat、arange 和其他东西,但没有找到任何突破口。 And if there is a way to get the job done faster, can you guys show me the way?如果有一种方法可以更快地完成工作,你们能告诉我方法吗?

Thank you guys in advance.提前谢谢你们。

If it is still of interest to you, you could get rid of one for loop and use numpy.arange() in combination with list comprehension and numpy.hstack() to get what is desired.如果您仍然对它感兴趣,您可以去掉一个for循环并使用numpy.arange()结合列表理解和numpy.hstack()来获得所需的内容。 Having said that we would still need at least one for loop to get this done (because neither range nor arange accept a sequence of endpoints)话虽如此,我们仍然需要至少一个for循环来完成这项工作(因为rangearange都不接受arange端点)

t1 = [0,13,22]
t2 = [4,14,25]
np.hstack([np.arange(r[0], r[1]+1)  for r in zip(t1, t2)])

# outputs
array([ 0,  1,  2,  3,  4, 13, 14, 22, 23, 24, 25])

However, I don't know how much more performant this is going to be for your specific case.但是,我不知道对于您的特定情况,这会提高多少性能。

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

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