Operating on a list of lists with the conditional

Asked

Viewed 74 times

0

I’m trying to use list comprehension in a code but I "zipped" in using the structures for and if in a certain part. Can someone help me? Basically I have a list called "sup" whose 2000 entries are also lists of various lengths. I have another list of lists called "gap" (also with 2000 entries and whose element-lists are the same length as the "sup" list. I must "delete" every element in "sup" whose corresponding "gap" is equal to 0. (For example, sup[i][j] must be deleted from sup[i] if gap[i][j] is equal to zero for all i and j). The two lists "sup" and "gap" have "float" type entries although the "gap" only contains zeros and ones.

Of all that I tried to write the only script that did not error syntax (use python 3 in Spyder) was the following but that also does not work.

    sup_new = [[sup[i].remove(int(j)) for j in gap[i] if gap[i][int(j)] > 0] for i in range(len(sup))]

But the return is

Valueerror: list.remove(x): x not in list

Can someone show me where I’m going wrong? Thank you.

1 answer

2

The error is that you are trying to remove what does not exist. By running, for example, [1, 2].remove(3), will give the cited error, because there is no way to remove the number 3 from the list [1, 2]. Are you trying to remove the int(j) of sup[i], but it does not exist. Another detail is that the return of list.remove is None, for he modifies the list itself, so by using it within the list comprehension you would be creating a list only of None, not with filtered lists.

Python already has a function that filters a sequence based on another sequence: itertools.compress.

from itertools import compress

x = ['a', 'b', 'c']
y = [1.0, 0.0, 1.0]

result = compress(x, y)

print(list(result))  # ['a', 'c']

In the above example, 'b' is not returned because in y the value is 0.0.

So just do:

sup_new = [list(compress(s, g)) for s, g in zip(sup, gap)]

The list in list(compress(...)) only to force it to be a list instead of an iterator of the type itertools.compress.

About the class zip, read how Associate two lists in python

  • Woss, thanks for the help. I’ve never read about these list associations. I’ll read about it. Thank you very much"

  • @Lucianomagrini I added at the beginning of the answer a description of what did wrong too...

  • Woss, thank you again. I’m reading about this list association and this zip function I didn’t know about. It makes perfect sense your description of my error. I entered your suggestion in my code but it returns a syntax error. See: sup_new = [list(compress(s, g)) for zip(sup, gap)] whose return is File "<ipython-input-335-670598897d6d>", line 1&#xA; sup_new = [list(compress(s, g)) for zip(sup, gap)]&#xA;SyntaxError: invalid syntax

  • @Lucianomagrini The code was corrected in the answer already... was really wrong

  • Thank you. Really missed the loopings rs. I’ve been racking my brain about it for days. I think I will stop everything I am doing and I will read Tod the documentation on lists first. Rewind a house to gain time later. Really, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.