【发布时间】:2017-01-03 18:50:08
【问题描述】:
我有这个包含 5 个数字序列的列表:
['123', '134', '234', '214', '223']
并且我想获得每个数字1, 2, 3, 4 在每个数字序列的ith 位置的百分比。比如这个5数字序列在0th位置的数字是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的每个数字的元素的百分比,然后得到想要的结果。任何建议表示赞赏!
【问题讨论】:
标签: python list python-3.x