繁体   English   中英

Python - 是否可以将insert()插入到列表中的列表中?

[英]Python - is it possible to insert() into a list inside of a list?

在Python中,是否可以在列表中的列表中插入值?

例如:

    List = [['Name',[1, 4, 6]],
    ['Another Name', [1,2,5]]]

我试过用:

    List.insert([0][1], 'another value')

但它不喜欢那样,是否有另一种方法来操纵列表中的列表?

绝对有可能:

>>> 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]]]

您只需要下标“外部”列表以获取对要插入的“内部”列表的引用。

我们可以将上面的代码分解为以下步骤:

inner = List[0]
inner.insert(1,'Another Value')

如果这让你更清楚我在那里做了什么......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM