[英]Is it possible to insert a value into the last index of a list in python?
[英]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.