简体   繁体   English

将值添加/附加到列表中的特定索引

[英]Add/Append value to specific index in a list

I have the following list:我有以下清单:

a = [['628', 'facial-expressions_2868588k.jpg', 'anger', '2'], ['628', 'facial-expressions_2868585k.jpg', 'surprise', '5'], ['628', 'facial-expressions_2868584k.jpg', 'disgust', '6'], ['628', 'facial-expressions_2868582k.jpg', 'fear', '4'], ['dwdii', 'Aaron_Eckhart_0001.jpg', 'neutral', '3'], ['302', 'Aaron_Guiel_0001.jpg', 'happiness', '1'], ['302', 'Aaron_Patterson_0001.jpg', 'neutral', '3'], ['302', 'Aaron_Peirsol_0001.jpg', 'happiness', '1'], ['302', 'Aaron_Peirsol_0002.jpg', 'happiness', '1'], ['302', 'Aaron_Peirsol_0003.jpg', 'happiness', '1'], ['302', 'Aaron_Peirsol_0004.jpg', 'neutral', '3'], ['302', 'Aaron_Pena_0001.jpg', 'neutral', '3']]

I want to append/add values to the indexes of this list.我想向此列表的索引附加/添加值。 Let's say I want to append 'hi' to the first index of this list to get the following:假设我想将'hi'附加到此列表的第一个索引以获得以下内容:

a = [['628', 'facial-expressions_2868588k.jpg', 'anger', '2', 'hi'], ['628', 'facial-expressions_2868585k.jpg', 'surprise', '5'], ['628', 'facial-expressions_2868584k.jpg', 'disgust', '6'], ['628', 'facial-expressions_2868582k.jpg', 'fear', '4'], ['dwdii', 'Aaron_Eckhart_0001.jpg', 'neutral', '3'], ['302', 'Aaron_Guiel_0001.jpg', 'happiness', '1'], ['302', 'Aaron_Patterson_0001.jpg', 'neutral', '3'], ['302', 'Aaron_Peirsol_0001.jpg', 'happiness', '1'], ['302', 'Aaron_Peirsol_0002.jpg', 'happiness', '1'], ['302', 'Aaron_Peirsol_0003.jpg', 'happiness', '1'], ['302', 'Aaron_Peirsol_0004.jpg', 'neutral', '3'], ['302', 'Aaron_Pena_0001.jpg', 'neutral', '3']]

How can I do that?我怎样才能做到这一点?

执行a[0].append('hi')因为您想要的列表是列表a的第 0 个索引。

a[0].append('hi') . a[0].append('hi') Just go from the outer index number to the inner one.只需从外部索引号转到内部索引号。 For example if you have the list below:例如,如果您有以下列表:

num_list = [1, [2, 3], [4, [5], 6]]

Imagine you want to add 0 to have the following:想象一下,您想添加0以获得以下内容:

num_list = [1, [2, 3], [4, [5, 0], 6]]

In order to do so, you need to do this:为此,您需要执行以下操作:

num_list[2][1].append(0)

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

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