[英]Python - is it possible to insert() into a list inside of a list?
In Python is it possible to insert a value inside a list that's inside a list? 在Python中,是否可以在列表中的列表中插入值?
for example: 例如:
List = [['Name',[1, 4, 6]],
['Another Name', [1,2,5]]]
I've tried to use: 我试过用:
List.insert([0][1], 'another value')
but it doesn't like that, is there another way to manipulate lists inside lists? 但它不喜欢那样,是否有另一种方法来操纵列表中的列表?
Definitely possible: 绝对有可能:
>>> List = [['Name',[1, 4, 6]],
... ['Another Name', [1,2,5]]]
>>> List[0].insert(1,"Another Value")
>>> List
[['Name', 'Another Value', [1, 4, 6]], ['Another Name', [1, 2, 5]]]
You just need to subscript the "outer" list to get a reference to the "inner" list that you want to insert into. 您只需要下标“外部”列表以获取对要插入的“内部”列表的引用。
We could break the above code into the following steps: 我们可以将上面的代码分解为以下步骤:
inner = List[0]
inner.insert(1,'Another Value')
If that makes it more clear to you what I actually did there ... 如果这让你更清楚我在那里做了什么......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.