下面是两个列表列表,我试图创建一个sum_list
,它将汇总每个列表中的每个索引,然后将其附加到一个列表(sum_list
)
e.g。 over_50
list = list1(row[0])
= 52 + list2(row[0])
= 31 .....总sum_list(row[0])
= 83
然后附加到under_50
sums_list
over_50k = [52, 0.09233516133146283, None, None, 9, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.9076648386685372, 0.8496365259533223, 0, 0, 45, None], [31, 0.6329549802321133, None, None, 14, 0.06261956383114399, 0.23708710623644944, 0.10916974875653615, 0.9076648386685372, 0.15036347404667771, 14084, 0, 50, None], [42, 0.6329549802321133, None, None, 13, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.9076648386685372, 0.8496365259533223, 5178, 0, 40, None], [37, 0.6329549802321133, None, None, 10, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.04935594949623773, 0.8496365259533223, 0, 0, 80, None], [30, 0.04501976788674914, None, None, 13, 0.8534625685499299, 0.23708710623644944, 0.7547506695574544, 0.0351995918887897, 0.8496365259533223, 0, 0, 40, None], [40, 0.6329549802321133, None, None, 11, 0.8534625685499299, 0.1184797857416146, 0.7547506695574544, 0.0351995918887897, 0.8496365259533223, 0, 0, 40, None]
under_50k = [52, 0.09233516133146283, None, None, 9, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.9076648386685372, 0.8496365259533223, 0, 0, 45, None], [31, 0.6329549802321133, None, None, 14, 0.06261956383114399, 0.23708710623644944, 0.10916974875653615, 0.9076648386685372, 0.15036347404667771, 14084, 0, 50, None], [42, 0.6329549802321133, None, None, 13, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.9076648386685372, 0.8496365259533223, 5178, 0, 40, None], [37, 0.6329549802321133, None, None, 10, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.04935594949623773, 0.8496365259533223, 0, 0, 80, None], [30, 0.04501976788674914, None, None, 13, 0.8534625685499299, 0.23708710623644944, 0.7547506695574544, 0.0351995918887897, 0.8496365259533223, 0, 0, 40, None], [40, 0.6329549802321133, None, None, 11, 0.8534625685499299, 0.1184797857416146, 0.7547506695574544, 0.0351995918887897, 0.8496365259533223, 0, 0, 40, None]
我的代码如下:
def sums_list(under_50k, over_50k):
sums_list = []
for index in range(14):
sums_list.append(under_50k[index]+over_50k[index])
print(sums_list)
return(sums_list)
如果有人可以让我为我工作,我会非常感激,现在已经看了一会儿!
答案 0 :(得分:0)
这可以通过 numpy :
轻松完成>>> import numpy as np
>>> over_50k = [52, 0.09233516133146283, None, None, 9, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.9076648386685372, 0.8496365259533223, 0, 0, 45, None], [31, 0.6329549802321133, None, None, 14, 0.06261956383114399, 0.23708710623644944, 0.10916974875653615, 0.9076648386685372, 0.15036347404667771, 14084, 0, 50, None], [42, 0.6329549802321133, None, None, 13, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.9076648386685372, 0.8496365259533223, 5178, 0, 40, None], [37, 0.6329549802321133, None, None, 10, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.04935594949623773, 0.8496365259533223, 0, 0, 80, None], [30, 0.04501976788674914, None, None, 13, 0.8534625685499299, 0.23708710623644944, 0.7547506695574544, 0.0351995918887897, 0.8496365259533223, 0, 0, 40, None], [40, 0.6329549802321133, None, None, 11, 0.8534625685499299, 0.1184797857416146, 0.7547506695574544, 0.0351995918887897, 0.8496365259533223, 0, 0, 40, None]
>>>
>>> under_50k = [52, 0.09233516133146283, None, None, 9, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.9076648386685372, 0.8496365259533223, 0, 0, 45, None], [31, 0.6329549802321133, None, None, 14, 0.06261956383114399, 0.23708710623644944, 0.10916974875653615, 0.9076648386685372, 0.15036347404667771, 14084, 0, 50, None], [42, 0.6329549802321133, None, None, 13, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.9076648386685372, 0.8496365259533223, 5178, 0, 40, None], [37, 0.6329549802321133, None, None, 10, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.04935594949623773, 0.8496365259533223, 0, 0, 80, None], [30, 0.04501976788674914, None, None, 13, 0.8534625685499299, 0.23708710623644944, 0.7547506695574544, 0.0351995918887897, 0.8496365259533223, 0, 0, 40, None], [40, 0.6329549802321133, None, None, 11, 0.8534625685499299, 0.1184797857416146, 0.7547506695574544, 0.0351995918887897, 0.8496365259533223, 0, 0, 40, None]
>>> over_50k=np.array(over_50k,dtype=float) # dtype=float converts None to nan
>>> under_50k=np.array(under_50k,dtype=float)
>>> over_50k=np.nan_to_num(over_50k) # np.nan_to_num() converts nan to zero
>>> under_50k=np.nan_to_num(under_50k)
>>> over_50k_sum=sum(over_50k)
>>> under_50k_sum=list(sum(under_50k))
>>> under_50k_sum=list(sum(under_50k))
>>> under_50k_sum
[232.0, 2.669174850146665, 0.0, 0.0, 70.0, 4.3299324065807934, 1.3456191812268843, 3.8829230965438084, 2.8427496492794284, 4.3985461038132891, 19262.0, 0.0, 295.0, 0.0]
>>> over_50k_sum
[232.0, 2.669174850146665, 0.0, 0.0, 70.0, 4.3299324065807934, 1.3456191812268843, 3.8829230965438084, 2.8427496492794284, 4.3985461038132891, 19262.0, 0.0, 295.0, 0.0]
答案 1 :(得分:0)
over_50k = [[52, 0.09233516133146283, None, None, 9, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.9076648386685372, 0.8496365259533223, 0, 0, 45, None], [31, 0.6329549802321133, None, None, 14, 0.06261956383114399, 0.23708710623644944, 0.10916974875653615, 0.9076648386685372, 0.15036347404667771, 14084, 0, 50, None], [42, 0.6329549802321133, None, None, 13, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.9076648386685372, 0.8496365259533223, 5178, 0, 40, None], [37, 0.6329549802321133, None, None, 10, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.04935594949623773, 0.8496365259533223, 0, 0, 80, None], [30, 0.04501976788674914, None, None, 13, 0.8534625685499299, 0.23708710623644944, 0.7547506695574544, 0.0351995918887897, 0.8496365259533223, 0, 0, 40, None], [40, 0.6329549802321133, None, None, 11, 0.8534625685499299, 0.1184797857416146, 0.7547506695574544, 0.0351995918887897, 0.8496365259533223, 0, 0, 40, None]]
under_50k = [[52, 0.09233516133146283, None, None, 9, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.9076648386685372, 0.8496365259533223, 0, 0, 45, None], [31, 0.6329549802321133, None, None, 14, 0.06261956383114399, 0.23708710623644944, 0.10916974875653615, 0.9076648386685372, 0.15036347404667771, 14084, 0, 50, None], [42, 0.6329549802321133, None, None, 13, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.9076648386685372, 0.8496365259533223, 5178, 0, 40, None], [37, 0.6329549802321133, None, None, 10, 0.8534625685499299, 0.25098839433745695, 0.7547506695574544, 0.04935594949623773, 0.8496365259533223, 0, 0, 80, None], [30, 0.04501976788674914, None, None, 13, 0.8534625685499299, 0.23708710623644944, 0.7547506695574544, 0.0351995918887897, 0.8496365259533223, 0, 0, 40, None], [40, 0.6329549802321133, None, None, 11, 0.8534625685499299, 0.1184797857416146, 0.7547506695574544, 0.0351995918887897, 0.8496365259533223, 0, 0, 40, None]]
from itertools import izip_longest,ifilterfalse
over_zipped = izip_longest(*over_50k,fillvalue=0)
under_zipped = izip_longest(*under_50k,fillvalue=0)
over = [sum(ifilterfalse(lambda x: x is None,x)) for x in over_zipped]
under =[sum(ifilterfalse(lambda x: x is None,x)) for x in under_zipped]
使用izip_longest
为fillvalue
的{{1}}将处理不均匀的长度列表,然后我们将每列与“{1}}删除无”。
ifilterfalse