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.
Woss, thanks for the help. I’ve never read about these list associations. I’ll read about it. Thank you very much"
– Luciano Magrini
@Lucianomagrini I added at the beginning of the answer a description of what did wrong too...
– Woss
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 isFile "<ipython-input-335-670598897d6d>", line 1
 sup_new = [list(compress(s, g)) for zip(sup, gap)]
SyntaxError: invalid syntax
– Luciano Magrini
@Lucianomagrini The code was corrected in the answer already... was really wrong
– Woss
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!
– Luciano Magrini