简体   繁体   English

查找大型数组列表的特定索引

[英]Finding the specific index of a list of large array

I have a list of large array of different lengths. 我有一系列不同长度的清单。 I want to find the position of each array larger than 420. For example, [size=(134,7),size=(620,7), size=(800,7),......] 我想找到每个大于420的数组的位置。例如,[size =(134,7),size =(620,7),size =(800,7),......]

My code is below 我的代码如下

for x in x_train:
       if len(x)>420:
           print(x_train.index(x))

I got this error: 我收到此错误:

DeprecationWarning: elementwise == comparison failed; this will raise an 
error in the future.

What will be the correct solution? 什么是正确的解决方案?

That's a warning that something has been deprecated, but is not an error. 这是警告某些东西已被弃用,但这不是错误。 I highly doubt your code block produced this warning, and chances are some imported library or other code was the cause. 我高度怀疑您的代码块会产生此警告,并且可能是某些导入的库或其他代码引起的。

Anyways, about your code, it is extremely inefficient, as once you find x you are looking for it again in the list. 无论如何,关于您的代码,它效率极低,因为一旦找到x您就会在列表中再次寻找它。 Use enumerate to get the index instead: 使用enumerate获取索引:

for i, x in enumerate(x_train):
    if len(x) > 420:
        print(i)

With a list comprehension, you can store all of the indexes: 使用列表推导,您可以存储所有索引:

indexes = [i for i, x in enumerate(x_train) if len(x) > 420]

Let's use List comprehension to solve it. 让我们使用列表理解来解决它。

#Create a sample list of arrays of arbitrary length.
myList = [np.full((134,7), 10), np.full((620,7),0), np.full((800,7),0), np.full((150,7),0), np.full((500,7),0)]

indexes_gt_420 = [i for i in range(len(myList)) if len(myList[i]) > 420]
indexes_gt_420
     [1, 2, 4]

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

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