首页 > 解决方案 > 从嵌套列表中删除不需要的文本

问题描述

我希望能够删除一些我不想在我的列表中出现的单词。

代码:

contour = cnt
stopwords = ['array', 'dtype=int32']

for word in list(contour):
    if word in stopwords:
        contour.remove(word)

print(contour)

输出:

[array([[[21, 21]],

       [[21, 90]],

       [[90, 90]],

       [[90, 21]]], dtype=int32)]

FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison if word in stopwords:

如何删除dtype=int32and array,同时使列表只是两点的列表?

前任:

[[21, 21],

 [21, 90],

 [90, 90],

 [90, 21]]

标签: python

解决方案


使用numpy.ndarray.tolist()

import numpy as np

l = np.array(
    [np.array([[[21, 21]],

       [[21, 90]],

       [[90, 90]],

       [[90, 21]]], dtype="int32")]
)
l.tolist()

输出:

[[[[21, 21]], [[21, 90]], [[90, 90]], [[90, 21]]]]

推荐阅读