我希望能够删除列表中不需要的一些单词。

码:

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=int32array,同时使列表仅是两点的列表?

例如:

[[21, 21],

 [21, 90],

 [90, 90],

 [90, 21]]

最佳答案

使用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]]]]

关于python - 从嵌套列表中删除不需要的文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56856861/

10-11 22:24
查看更多