从嵌套lis中删除不需要的文本

2024-04-27 05:07:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望能够删除一些单词,我不想在我的名单内

代码:

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

Tags: 代码in列表if单词arraycomparisonword
1条回答
网友
1楼 · 发布于 2024-04-27 05:07:52

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

相关问题 更多 >