2016-06-14 296 views
-1

我想问一下如何在python列表中添加多个变量到列表中。在Python列表中添加多个变量到列表中

yearmonthlst = [[] for i in range(64)] 
precipitation = [197.6, 95.0, 37.1, 74.2, 65.3, 175.5, 114.6, 90.4, 26.7, 62.2, 58.9, 142.0, 129.0, 122.2...] 

因此,例如在这里我有一个列表里面有64个其他列表。然后我有另一个有很多值的列表。对于沉淀的每个值,直到13日它会被添加到列表yearmonthlst

[197.6, 95.0, 37.1, 74.2, 65.3, 175.5, 114.6, 90.4, 26.7, 62.2, 58.9, 142.0], ['''12 values within this list'''], ['''12 values within this list''']...] 

内我想有在yearmonthlst内的每一个列表降水12个值。所以它从降水[0]开始到[11]将会附加到yearmonthlst [0],然后迭代另一个列表yearmonthlst [1],继续从已经在降水中迭代的值, 等值[12] - [23]追加到该列表中。

回答

0
[precipitation[i:i+12] for i in range(0, len(precipitation), 12)] 

输入:

precipitation = [197.6, 95.0, 37.1, 74.2, 65.3, 175.5, 114.6, 90.4, 26.7, 62.2, 58.9, 142.0, 129.0, 122.2] 

输出:

[[197.6, 95.0, 37.1, 74.2, 65.3, 175.5, 114.6, 90.4, 26.7, 62.2, 58.9, 142.0], [129.0, 122.2]] 
0

这应该工作:

for index, value in enumerate(precipitation): 
    yearmonthlst[index % 12].append(value) 
相关问题