如何将值添加到Python嵌套列表?

时间:2017-06-19 01:57:48

标签: python

如何为每个列表添加值?

之前:

[['2010', '10', '24', '20', '32', '18', '5'], ['2020', '16', '22', '23', '30', '16', '9'], ['2030', '28', '19', '29', '30', '12', '13']]

结果:[list] value add''

[['2010', '10', '24', '20', '32', '18', '5', ''], ['2020', '16', '22', '23', '30', '16', '9', ''], ['2030', '28', '19', '29', '30', '12', '13', '']]

2 个答案:

答案 0 :(得分:3)

用于循环,

nested_list = [['2010', '10', '24', '20', '32', '18', '5'], ['2020', '16', '22', '23', '30', '16', '9'], ['2030', '28', '19', '29', '30', '12', '13']]

for a_list in nested_list:
    a_list.append('')

答案 1 :(得分:2)

你也可以这样做:

my_list = [['2010', '10', '24', '20', '32', '18', '5'], 
           ['2020', '16', '22', '23', '30', '16', '9'],
           ['2030', '28', '19', '29', '30', '12', '13']]

my_list = [inner_list + [''] for inner_list in my_list]
my_list

输出:

[['2010', '10', '24', '20', '32', '18', '5', ''],
 ['2020', '16', '22', '23', '30', '16', '9', ''],
 ['2030', '28', '19', '29', '30', '12', '13', '']]