python - 从数组中删除项目

标签 python list

我有一个类似于 [0 0 0]、[3 3 3]、[255 255 255](数字是 RGB 值)的数组,我想删除所有具有值 [0 0 0 ] 结果-->[3 3 3],[5 5 5]。为了实现我的不同算法,首先我必须使我的数组成为一个列表(使用 .tolist 方法),它返回我一个这样的列表:"[0, 0, 0]","[3,3,3 ]","[255, 255, 255]"。 那么第一个问题:[0 0 0]和“[0,0,0]”有什么区别吗?

此外,我还使用了此处许多主题中提出的不同算法,例如:

for n,i in enumerate(mylist)
    if 0 in i: mylist[n].remove(0)

def clear_list_from_item(mylist, item):
    try:
       while True: mylist.remove(item)
    except ValueError:
       return mylist

mylist = [clear_list_from_item(x, 0) for x in mylist]

它们都没有返回结果。所以我的第二个问题:为什么?是因为我的列表,因为当我应用它时,让我们说在列表中像 =[[3,4],[5],[6,7,8]] 所有这些都很好.

更新:1)嵌套级别:269x562(即我正在使用的图片的高度x宽度,因此我得到每个像素的列表) 2).tolist() 方法之前数组第一行的 18 个值的初始数组

[[  0   0   0]
[255 255 255]
[255 255 255]
[255 255 255]
[255 255 255]
[250 250 250]
[255 255 255]
[255 255 255]
[255 255 255]
[255 255 255]
[  0   0   0]
[  0   0   0]
[  0   0   0]
[  0   0   0]
[  0   0   0]
[  0   0   0]
[  0   0   0]
[  0   0   0]

最佳答案

mylist = filter(lambda rgb: any(rgb), mylist)

编辑

它可以更好:

mylist = filter(any, mylist)

关于python - 从数组中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25032940/

相关文章:

Python 字典列表 - 添加具有相同键名的字典

python - 导入pyodbc时无法执行.pyw文件?

python - argparse 一个参数而不是其他几个参数

python - 按键、值(元组)对字典进行排序

python - Python 中的 ISO 时间 (ISO 8601)

c - 从链表末端删除节点

python - 使用两个参数按字母顺序对列表中的元组进行排序

python - 通过正则表达式从列表中的字符串中提取数字

list - haskell,计算列表中有多少个素数

python - xlwings 调用一个应该创建文件的 python 函数,但没有创建文件