how to remove an element from a nested list?

后端 未结 8 1161
一生所求
一生所求 2020-12-17 00:37

if i have an nested list such as:

m=[[34,345,232],[23,343,342]]

if i write m.remove(345) it gives an error message saying elem

8条回答
  •  攒了一身酷
    2020-12-17 01:21

    In [5]: m=[[34,345,232],[23,343,342]]
    
    In [7]: [[ subelt for subelt in elt if subelt != 345 ] for elt in m] 
    Out[7]: [[34, 232], [23, 343, 342]]
    

    Note that remove(345) only removes the first occurrance of of 345 (if it exists). The above code removes all occurrances of 345.

提交回复
热议问题