2

I have an array which is like [0 0 0],[3 3 3],[255 255 255](the numbers are RGB values) and I want to remove all the items that has the values [0 0 0] and as result-->[3 3 3],[5 5 5]. So as to implement my different algorithms first of all I have to make my array a list(using .tolist method) and it returns me a list like that:"[0, 0, 0]","[3,3,3]","[255, 255, 255]". So 1st question:Is there any difference between [0 0 0] and "[0, 0, 0]"?

In addition I have use different algorithms as proposed in many topics here like:

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

or

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]

And none of them is returning a result. So my 2nd question: Why?Is it because of my list, because when I apply it let's say in list like a =[[3,4],[5],[6,7,8]] all of them work great.

update:1)Level of nesting:269x562(i.e the heightxwidth of the pic that I am working with so I get a list for each pixel) 2)My initial array of 18 values of the first row of the array before .tolist() method

[[  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]
DimKoim
  • 1,024
  • 6
  • 20
  • 33

4 Answers4

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

EDIT

it can be even better:

mylist = filter(any, mylist)
Bartosz Marcinkowski
  • 6,651
  • 4
  • 39
  • 69
  • done like that:filter(lambda a: a != 0, mylist) but no result – DimKoim Jul 30 '14 at 09:21
  • Note that `[x for x in mylist if any(x)]` is similar, and sometimes preferred. – Joost Jul 30 '14 at 09:26
  • @Miko `a != 0` is not the same as `any(a)`. `a != 0` does not make sense, since `a` is a list. `any(a)` rests whether any of the elements _in_ `a` (the RGB values) is `0`. – tobias_k Jul 30 '14 at 09:40
  • I implement as:my_new_list=[x for x in my_old_list if any(0)] and get the following error:TypeError: 'int' object is not iterable – DimKoim Jul 30 '14 at 11:39
  • @DimKoim I'm sorry for my short temper, but that's just too annoying. You have at least 4 correct solutions in answers and comments, and still you try to correct them and actually make them incorrect, without good understanding of what you are doing. You did not do through enough tutorials to solve it by yourself - shame. You got ready-made answers for your problem, change them (like use of `any`), obviously without checking what `any` does in the docs - oh come on, just show some effort. – Bartosz Marcinkowski Jul 30 '14 at 11:46
  • @BartoszMarcinkowski You can criticize me about anything(i.e.level of understudying, programming skills...) but please stop for the effort that I put. In addition, I checked about everything, I checked for all the proposed solutions but I am newbie and need more time. As an experienced you should be more patient in my opinion. I am speaking without any temper and I thank you for your help. Regards. – DimKoim Jul 30 '14 at 12:10
2

Let's say you have the follwing source list:

>>> my_nested_list = [[0, 0, 0], [3, 3 ,3], [255, 255, 555]]

There's several way to delete the entry that contains all the zeroes. A nice way is to use a list comprehension which gonna create a new list containing only the values you want:

>>> my_new_list = [sublist for sublist in my_nested_list if sublist != [0, 0, 0]]
>>> my_new_list
[[3, 3 ,3], [255, 255, 555]]

Note that with this method you do not mutate (modify the initial list) which can be handy if you have to do more processing on it later, the downside of this method is that you end up taking more memory (as you're creating a new list).

If you really want to modify your source list there are several way to do it, one of them includes include the use of del:

def delete_stuff_in_my_list(m_list, pattern=[0, 0, 0]):
    for index, value in enumerate(m_list):
        if value == pattern:
            del m_list[index]

In this case:

>>> delete_stuff_in_my_list(my_nested_list)
>>> my_nested_list
[[3, 3, 3], [255, 255, 255]]

BTW there's a great explanation on the different delete options available here

Community
  • 1
  • 1
Ketouem
  • 3,820
  • 1
  • 19
  • 29
  • In both cases print min(my_nested_list)--->[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0],.... I dunno know why and how to solve it. – DimKoim Jul 30 '14 at 10:01
  • That's weird. Could you add the original list (or at least its full structure, level of nesting) inside your original post please ? – Ketouem Jul 30 '14 at 10:03
  • I am a newbie so how can I ''find'' the level of nesting? Moreover, is it more helpful to attach two .csv files one with my initial array and one with the return of the .tolist()? – DimKoim Jul 30 '14 at 10:08
  • By nesting I mean how many 'list inside list'. And yes do post the initial list you're working with (or a usable slice if is too big)? – Ketouem Jul 30 '14 at 10:12
0

You should modify your first function with:

mylist = [[0, 0, 0],[3, 3, 3],[255, 255, 255]]
for n,i in enumerate(mylist):
    if i == [0,0,0]:
         mylist.pop(n) # pop remove object at position n in-place
         # or with : mylist.remove(i)

Note that using pop() or remove() will create a pb in the index of the list and if you print i before the if it will print : [0, 0, 0],[255, 255, 255] but not [3, 3, 3]

So you should prefered something not in-place like:

my_new_list = [i for i in mylist if i!=[0,0,0]]

or if you have an array at the beginning :

my_new_array = np.array([i for i in myarray if (i!=0).all()])

or

my_new_array = np.array([i for i in myarray if any(i)]) # but specific to remove array of 0s.

Hope this helps

jrjc
  • 21,103
  • 9
  • 64
  • 78
0

difference between [0 0 0] and "[0, 0, 0]"? [0 0 0] is nothing in python unless you put a comma between the zeros in which case it becomes a list. "[0,0,0]" is a string.

t = [[0, 0, 0],[3, 3, 3],[255, 255, 255]]
[f for f in t if f != [0,0,0]]

Will give you a list of lists which are not 0,0,0

I must say your question is not clear. In case my answer is not what you are looking for please tell me what I am missing.

Akilesh
  • 1,252
  • 1
  • 9
  • 16
  • Ok I should let you know about the procedure that I have followed till now:I have an Image. I open it im = Image.open('myimage.jpg').Than I get the pixels values from my image using pix = np.array(im). But I want a specific ROI so I create an array with zeros roipix = np.zeros_like(pix) and the specify the region that I want to keep pixel values:roipix[start:end:step,start:end:step]= allpix[start:end:step,start:end:step].print roipix--->[[ 0 0 0] [250 250 250] [255 255 255]... – DimKoim Jul 30 '14 at 09:40
  • Copying from http://stackoverflow.com/questions/21662532/python-list-indices-must-be-integers-not-tuple: ''The problem is that [...] in python has two distinct meanings expr [ index ] means accessing an element of a list [ expr1, expr2, expr3 ] means building a list of three elements from three expressions. Makes sense but cannot understand how is correlated to my problem. – DimKoim Jul 30 '14 at 11:41