2017-06-19 71 views
2

如何爲每個列表添加值?如何將值添加到Python嵌套列表?

前:

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

結果:[名單]附加價值'

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

回答

2

使用for循環,

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('') 
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', '']]