【问题标题】:Loop through tuple and calculate percentage of a number循环遍历元组并计算数字的百分比
【发布时间】:2016-12-08 09:56:00
【问题描述】:

我正在尝试遍历下面的tuple (tuple1),从整数 (b) 创建一个总数,计算一个百分比每个值表示,并将其与变量 a 一起存储在一个新变量中 (a_percent)

我尝试了以下方法:

tuple1 = [('Data1', 33), ('Data2', 52), ('Data3', 85)]

total = b
for a, b in tuple1:
    total += b
    y = b/total*100.0
    a_percent = a, y

a_percent 我的预期输出是例如对于Data1:(33/(33+52+85)*100):

Data1, 19.4 ...   
Data2, 30.5 ...
Data3, 50

但是,它显示如下:

('Data1', 100.0)
('Data2', 0.0)
('Data3', 0.0)

【问题讨论】:

  • x 来自哪里?
  • @AriGold 我的错误,x 应该是 total
  • and y "....y = b/y*100.0....", and "total = b"?
  • @AriGold 已修复,谢谢。
  • sry 但还是有问题,“total = b”是什么意思?

标签: python python-2.7 list tuples


【解决方案1】:

加入您正在尝试的基本解决方案。可以帮助您轻松理解这一点。

tuple1 = [('Data1', 33), ('Data2', 52), ('Data3', 85)]

total = 0
for a, b in tuple1:
    total += int(b)

for a,b in tuple1:
    y = float(int(b)) * 100 /total
    a_percent = a, y
    print a_percent

输出:

('Data1', 19.41176470588235)
('Data2', 30.58823529411765)
('Data3', 50.0)

【讨论】:

  • @AlexC。请不要对这个答案进行重大更改,以免偏离该用户试图传达的内容。这种行为在 Stack Overflow 上是不可接受的:meta.stackoverflow.com/questions/260245/…
  • @rayryeng 没有什么会偏离我想要传达的内容。我发现这个答案最有用,如果有的话。
  • @rayryeng 是的,我暗示他的回答没有偏离我的问题。
【解决方案2】:

b/y 当两者都是整数时,会导致整数除法。之后乘以100.0 并不重要。要么float(b)/y*100.0要么b*100.0/y

尽管这段代码没有按照您的意愿进行操作,并且如果没有两次循环,您将无法获得每个值的百分比。相反,我可以建议

total = sum([t[1] for t in tuple1])
a_percent = [(t[0], float(t[1])/total) for t in tuple1]

【讨论】:

    【解决方案3】:

    您可以先获取total,然后循环计算百分比:

    >>> tuple1 = [('Data1', 33), ('Data2', 52), ('Data3', 85)]
    >>> total = sum(t[1] for t in tuple1)
    >>> tuple_new = [(x, float(y) * 100 /total) for x, y in tuple1]
    >>> tuple_new
    [('Data1', 19.41176470588235), ('Data2', 30.58823529411765), ('Data3', 50.0)]
    

    如果需要,您也可以在元组中包含实际计数:

    >>> [(x, y, float(y) * 100 /total) for x, y in tuple1]
    [('Data1', 33, 19.41176470588235), ('Data2', 52, 30.58823529411765), ('Data3', 85, 50.0)]
    

    【讨论】:

    • 在 for 循环中 t 是列表中的一个元组,例如('Data1' 33) 并且可以使用 0 索引系统获取元组中的元素,因此 t[0]'Data't[1]33。因此,首先我们通过对所有元组中的第一个元素求和来获得总数,然后在另一个循环中计算百分比。我假设您理解这一点,因为接受的答案还包含t[1]
    • 谢谢 :-) /TooShort
    【解决方案4】:
    [y[1] for y in tuple1]
    sum_tuple = sum([y[1] for y in tuple1])
    [(y[0], (float(y[1])/sum_tuple)*100) for y in tuple1]
    

    输出:

    [('Data1', 19.411764705882355), ('Data2', 30.58823529411765), ('Data3', 50.0)]
    

    【讨论】:

      【解决方案5】:

      一个简单的答案(尚未测试)

      total = 0
      for data, value in tuple1:
            total += value     #get the total
      
      for data,value in tuple1:
            print float(value) / total * 100
      

      【讨论】:

        【解决方案6】:

        另一个简单的应用程序:

        tuple1 = [('Data1', 33), ('Data2', 52), ('Data3', 85)]
        
        total = (sum(t[1] for t in tuple1))
        for a, b in tuple1:
            y = b * 100 / total
            a_percent = a, y
            print(a_percent)
        

        输出:

        ('Data1', 19.41176470588235)
        ('Data2', 30.58823529411765)
        ('Data3', 50.0)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-09-14
          • 2017-08-16
          • 2021-04-23
          • 2014-01-29
          • 2021-10-26
          • 2019-12-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多