如何从列表的第i个值的总和中获得列表中第i个值的百分比

时间:2019-05-06 11:25:33

标签: python list logic

在python中创建特定逻辑时,我面临着一个小挑战,我正在创建一个具有三个列表的数据集,我想计算列表中每个第i个值的百分比并创建新列表,这是我试图实现的例子

我有这三个列表

 good=[500,400,300]
 opportunity=[300,200,100]
 bad=[100,50,20]

我能够获得第i个值的总计列表

total=[x + y + z for x, y, z in zip(good, opportunity ,bad)]
##total=[900,650,420]

现在我希望将列表转换为百分比

good=[(500/900)*100,(400/900)*100,(300/900)*100]
opportunity=[(300/650)*100,(200/650)*100,(100/650)*100]
bad=[(100/420)*100,(50/420)*100,(20/420)*100]

4 个答案:

答案 0 :(得分:0)

good = [(x/total[0])*100 for x in good]
opportunity=[(x/total[1])*100 for x in opportunity]
bad=[(x/total[2])*100 for x in bad]

输出:

[55.55555555555556, 44.44444444444444, 33.33333333333333]
[46.15384615384615, 30.76923076923077, 15.384615384615385]
[23.809523809523807, 11.904761904761903, 4.761904761904762]

答案 1 :(得分:0)

您可以使用列表列表和numpy使此问题更易于解决

import numpy as np

#List of lists from good, opportunity and bad
li = [[500,400,300],[300,200,100],[100,50,20]]

#Convert list to numpy arr
arr = np.array(li)

#Calculate total
total=[x + y + z for x, y, z in zip(*li)]

#Use numpy.divide to divide each element of list by total
print([np.divide(li[idx], total[idx]) for idx in range(3)])

输出将是

[array([0.55555556, 0.44444444, 0.33333333]), 
array([0.46153846, 0.30769231, 0.15384615]), 
array([0.23809524, 0.11904762, 0.04761905])]

您还可以按如下方式将百分比分配回变量

perc_good, perc_opportunity, perc_bad = [np.divide(li[idx], total[idx]) for idx in range(3)]
print(list(perc_good))
print(list(perc_opportunity))
print(list(perc_bad))

输出将为

[0.5555555555555556, 0.4444444444444444, 0.3333333333333333]
[0.46153846153846156, 0.3076923076923077, 0.15384615384615385]
[0.23809523809523808, 0.11904761904761904, 0.047619047619047616]

答案 2 :(得分:0)

good=[500,400,300]
opportunity=[300,200,100]
bad=[100,50,20]

datasets = [good, opportunity, bad]
p_list = [[], [], []]

total=[x + y + z for x, y, z in zip(good, opportunity ,bad)]

for i in range(len(datasets)):
    for element in datasets[i]:
        p_list[i].append(element / total[i] * 100)

print(p_list)

#[[55.55555555555556, 44.44444444444444, 33.33333333333333], 
#[46.15384615384615, 30.76923076923077, 15.384615384615385], 
#[23.809523809523807, 11.904761904761903, 4.761904761904762]]

答案 3 :(得分:-1)

在您的示例中,除数是在列(第i个位置)上计算的,但将应用于行而不是列。我认为您实际上是打算将它们应用于列(例如good=[(500/900)*100,(400/650)*100,(300/420)*100]

一旦有了总计列表,就可以使用zip将每个值列表转换为百分比列表:

totals             = [ sum(v)  for v   in zip(good,opportunity,bad) ]
goodPercent        = [ 100*v/t for v,t in zip(good,totals) ]
opportunityPercent = [ 100*v/t for v,t in zip(opportunity,totals) ]
badPercent         = [ 100*v/t for v,t in zip(bad,totals) ]

# goodPercent:        [55.55555555555556,  61.53846153846154,  71.42857142857143]
# opportunityPercent: [33.333333333333336, 30.76923076923077,  23.80952380952381]
# badPercent:         [11.11111111111111,   7.6923076923076925, 4.761904761904762]

# (columns add up to 100%)

另一方面,如果要跨行使用百分比,则应分别处理每个列表:

goodPercent        = [ 100*v/t for t in [sum(good)]        for v in good         ]
opportunityPercent = [ 100*v/t for t in [sum(opportunity)] for v in opportunity  ]
badPercent         = [ 100*v/t for t in [sum(bad)]         for v in bad          ]

# goodPercent:        [41.666666666666664, 33.333333333333336, 25.0]
# opportunityPercent: [50.0, 33.333333333333336, 16.666666666666668]
# badPercent:         [58.8235294117647, 29.41176470588235, 11.764705882352942]

# (rows add up to 100%)