[英]how to remove an item in a list of list of string using index in python
input:输入:
letters = [['a', 'b', 'c'], ['a', 'b', 'c']]
row= 0
column=0
output output
[['b', 'c'], ['a', 'b', 'c']]
So I tried to do this:所以我试着这样做:
letters[0].remove(0)
but it gives me a value error, while when I use pop like this:但它给了我一个值错误,而当我像这样使用 pop 时:
letters[0].pop(0)
it does what I need but the problem with pop is that it returns me something while I just need the item to be removed它可以满足我的需要,但是 pop 的问题是它返回给我一些东西,而我只需要删除该项目
letters = [['a', 'b', 'c'], ['a', 'b', 'c']]
del list1[0][0]
use the del keyword使用 del 关键字
list.remove()
removes an item by value, not by index. list.remove()
按值而不是按索引删除项目。 So letters[0].remove('b')
would work.letters[0].remove('b')
会起作用。del
which could be used as del letters[0][0]
del
可以用作del letters[0][0]
list.pop)
can return an element by index and remove it from the list.list.pop)
可以按索引返回一个元素并将其从列表中删除。You can try this if you want to delete the element by index value of the list:如果要按列表的索引值删除元素,可以试试这个:
del letters[0][0] # [['b', 'c'], ['a', 'b', 'c']]
del letters[1][0] # [['a', 'b', 'c'], ['b', 'c']]
or if you want remove specific value in the first occurance:或者如果您想在第一次出现时删除特定值:
letters[0].remove('a') # [['b', 'c'], ['a', 'b', 'c']]
letters[1].remove('a') # [['a', 'b', 'c'], ['b', 'c']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.