如何计算列表中每个元素的百分比?

时间:2017-01-03 18:50:09

标签: python list python-3.x

我的这个列表有5个数字序列:

['123', '134', '234', '214', '223'] 

我希望获得每个数字序列的1, 2, 3, 4位置中每个数字ith的百分比。例如,此0th数字序列的5位置的数字为1 1 2 2 2,那么我需要计算百分比 在此序列号中1, 2, 3, 4,并将百分比作为新列表的0th元素返回。

['123', '134', '234', '214', '223']

0th position: 1 1 2 2 2   the percentage of 1,2,3,4 are respectively: [0.4, 0.6, 0.0, 0.0]

1th position: 2 3 3 1 2   the percentage of 1,2,3,4 are respectively: [0.2, 0.4, 0.4, 0.0]

2th position: 3 4 4 4 3   the percentage of 1,2,3,4 are respectively: [0.0, 0.0, 0.4, 0.6]]

然后想要的结果是返回:

[[0.4, 0.6, 0.0, 0.0], [0.2, 0.4, 0.4, 0.0], [0.0, 0.0, 0.4, 0.6]]

到目前为止我的尝试:

list(zip(*['123', '134', '234', '214', '223']))

结果:

 [('1', '1', '2', '2', '2'), ('2', '3', '3', '1', '2'), ('3', '4', '4', '4', '3')]

但我被困在这里,然后我不知道如何计算每个1, 2, 3, 4数字的元素百分比,然后获得所需的结果。任何建议表示赞赏!

4 个答案:

答案 0 :(得分:3)

从您的方法开始,您可以使用Counter

完成剩下的工作
from collections import Counter

for item in zip(*['123', '134', '234', '214', '223']):
    c = Counter(item)
    total = sum(c.values())
    percent = {key: value/total for key, value in c.items()}
    print(percent)

    # convert to list
    percent_list = [percent.get(str(i), 0.0) for i in range(5)]
    print(percent_list)

打印

{'2': 0.6, '1': 0.4}
[0.0, 0.4, 0.6, 0.0, 0.0]
{'2': 0.4, '3': 0.4, '1': 0.2}
[0.0, 0.2, 0.4, 0.4, 0.0]
{'4': 0.6, '3': 0.4}
[0.0, 0.0, 0.0, 0.4, 0.6]

答案 1 :(得分:2)

您可以像创建压缩列表一样开始:

zipped = zip(*l)

然后将itertools.Counter映射到它,以便从zip获取结果中每个项目的计数:

counts = map(Counter, zipped)

然后通过它,创建一个列表,从他们的计数除以他们的大小:

res = [[c[i]/sum(c.values()) for i in '1234'] for c in counts]
print(res) 
[[0.4, 0.6, 0.0, 0.0], [0.2, 0.4, 0.4, 0.0], [0.0, 0.0, 0.4, 0.6]]

如果你是一个单行的人,在理解中将前两个问题排成一行:

res = [[c[i]/sum(c.values()) for i in '1234'] for c in map(Counter, zip(*l))]

另外,如评论中所述,如果您提前不知道这些元素,sorted(set(''.join(l)))可以替换'1234'

答案 2 :(得分:0)

您可以使用count(i)确定数字1-4的出现次数并将其除以5以获得百分比:

sequence=list(zip(*['123', '134', '234', '214', '223']))
percentages=[]
for x in sequence:
    t=list(x)
    temp=[t.count(str(i))/len(x) for i in range(1,5)]  #work out the percentage of each number
    percentages.append(temp) #add percentages to list

或者,作为一个列表理解:

percentages=[[list(x).count(str(i))/len(x) for i in range(1,5)]for x in sequence]

输出:

[[0.4, 0.6, 0.0, 0.0], [0.2, 0.4, 0.4, 0.0], [0.0, 0.0, 0.4, 0.6]]

答案 3 :(得分:0)

只是扩展@hiro 主角。 标记为评论的更改

from collections import Counter

lis = ['123', '134', '234', '214', '223'] 

symbols = {c for s in lis for c in s} # in case you take lis as input from user

percent_list = []

for item in zip(*lis):
    c = Counter(item)
    total = sum(c.values())
    percent = {key: value/total for key, value in c.items()}

    # convert to list, 0.0 is default value
    
    percent_list.append([percent.get(i, 0.0) for i in sorted(symbols)]) # Your comment even says. But then you don't make a list you create a generator.
    
    
sequence = list(zip(*lis))
sorted_s = list(sorted(symbols))


for index, item in enumerate(zip(*lis)):
    str_s_s = ','.join(sorted_s) # as pointed by @BallpointBen
    str_seque =  ' '.join(sequence[index])
    print(f"{index}th position: {str_seque} the percentage of {str_s_s} are respectively: {percent_list[index]}")

提供所需的输出

0th position: 1 1 2 2 2 the percentage of 1,2,3,4 are respectively: [0.4, 0.6, 0.0, 0.0]
1th position: 2 3 3 1 2 the percentage of 1,2,3,4 are respectively: [0.2, 0.4, 0.4, 0.0]
2th position: 3 4 4 4 3 the percentage of 1,2,3,4 are respectively: [0.0, 0.0, 0.4, 0.6]

[Program finished]