Given a List A
. I want to remove the last element ( b,'zz')
of each list with list A
.
A = [ [(a,'rr'),(c,'rr'),(d,'rr'),(b,'zz')],
[(g,'rr'),(h,'rr'),(e,'rr'),(b,'zz')],
[(c,'rr'),(r,'rr'),(s,'rr'),(b,'zz')] ]
What I tried.
for i in A:
N = del i[-1]
But it is not working.
You can use the list.pop()
method:
for i in A:
i.pop()
Reference: https://docs.python.org/2/tutorial/datastructures.html
你可以简单地切片直到列表理解中的最后一项:
A = [l[:-1] for l in A]
del
is a statement and must be used accordingly. Most importantly, it cannot be used in an assignment.
Simply remove the assignment:
for i in A:
del i[-1]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.