我想每十個元素在列表中添加元素。例如:添加列表中的某些元素
a = [5, 31, 16, 31, 19, 5, 25, 34, 8, 13, 17, 17, 43, 9, 29, 41, 8, 24,
48, 1, 28, 20, 37, 40, 32, 35, 9, 36, 17, 46, 10, 30, 49, 28, 2, 3, 8,
11, 36, 20, 7, 24, 29, 15, 0, 4, 35, 11, 42, 7, 28, 40, 31, 45, 6, 45,
15, 27, 39, 6]
所以我想創建一個每10個元素,比如總和一個新的列表:
new = [187, 237, 300, 197, 174, 282]
當第一個條目對應於添加了第10號:
x = sum(5, 31, 16, 31, 19, 5, 25, 34, 8, 13)
x = 187
第二個到10個號碼中的範圍10-19:
y = sum(17, 17, 43, 9, 29, 41, 8, 24, 48, 1)
y = 237
等;有沒有一種有效的方法來做到這一點?
可能的複製[?你怎麼分割成列表在Python均勻大小的塊(http://stackoverflow.com/questions/312443/how-do-you-split -a-list-into-uniformity-sized-chunk-in-python) – Mureinik