【问题标题】:Python code to sum a series incrementallyPython 代码对一系列增量求和
【发布时间】:2016-10-11 18:49:35
【问题描述】:

很棒的网站,我非常感谢我从这里得到的所有答案和提示。我正在尝试计算一系列 INCREMENTALLY 分数的总和,但似乎无法正确使用循环函数。我已经能够编写程序来计算总和,并计算系列中的各个分数,但我需要它来显示字符串中每个步骤的运行总和。该系列是 1/2、2/3、3/4、4/5、....、20/21。使其显示: 0.5000 1.1667 ... 16.4023 17.3546

这是我目前所拥有的:

def frac(n, d):
    a = n / d
    return a

def main():
    count = 1
    while count <= 20:
        num = count
        den = count +1
        ans = frac(num, den)
        print(num, "    ", format(ans, ".4f"))
        count += 1
main()

但它只给了我这个:

1      0.5000
2      0.6667
3      0.7500
4      0.8000
5      0.8333
6      0.8571
7      0.8750
8      0.8889
9      0.9000
10     0.9091
11     0.9167
12     0.9231
13     0.9286
14     0.9333
15     0.9375
16     0.9412
17     0.9444
18     0.9474
19     0.9500
20     0.9524

【问题讨论】:

    标签: python add series


    【解决方案1】:

    我根本不知道 python,但似乎您错过的步骤是在每次迭代中都考虑上一个答案。所以你可以这样做:

    def main():
        count = 1
        ans = 0
        while count <= 20:
            num = count
            den = count +1
            ans = ans + frac(num, den) #add previous ans here
            print(num, "    ", format(ans, ".4f"))
            count += 1 
    

    【讨论】:

      【解决方案2】:

      您也可以使用itertools.accumulate 来生成这些值:

      from itertools import accumulate
      for i, ans in enumerate(accumulate(n/(n+1) for n in range(1, 21)), start=1):
          print(str(i).ljust(4), format(ans, '.4f'))
      

      输出:

      1    0.5000
      2    1.1667
      3    1.9167
      4    2.7167
      5    3.5500
      6    4.4071
      7    5.2821
      8    6.1710
      9    7.0710
      10   7.9801
      11   8.8968
      12   9.8199
      13   10.7484
      14   11.6818
      15   12.6193
      16   13.5604
      17   14.5049
      18   15.4523
      19   16.4023
      20   17.3546
      

      【讨论】:

        【解决方案3】:

        正如许多人已经指出的那样,您错过了循环中部分和的累积。现在,稍微了解一下,并使用列表推导,同样可以这样实现:

        fractions = [ float(x)/(x+1) for x in range(1,21) ]
        cumulatives = [sum(fractions[0:i+1]) for i,j in enumerate(fractions)]
        
        for i,item in enumerate(cumulatives):
            print '{} {:.4f}'.format(i+1, item)
        

        这是在 python 2.7 中。 Python 3.x itertools 具有函数 accumulate。 在此处查看实际操作:https://eval.in/658781

        1 0.5000
        2 1.1667
        3 1.9167
        4 2.7167
        5 3.5500
        6 4.4071
        7 5.2821
        8 6.1710
        9 7.0710
        10 7.9801
        11 8.8968
        12 9.8199
        13 10.7484
        14 11.6818
        15 12.6193
        16 13.5604
        17 14.5049
        18 15.4523
        19 16.4023
        20 17.3546
        

        【讨论】:

          【解决方案4】:

          据我所知,您没有在任何地方跟踪您的总和,因此只需在循环之前添加一个 total 变量并添加到该变量(当 for 循环可以解决问题时,我也避免使用 while 循环) :

          def frac(n, d):
              a = n / d
              return a
          
          def main():
              total = 0
              for num in range(1, 21):
                  ans = frac(num, num+1)
                  total += ans
                  print(num, "    ", format(ans, ".4f"), 'total=', format(total, ".4f"))
          main()
          

          输出如下所示:

          1      0.5000 total= 0.5000
          2      0.6667 total= 1.1667
          3      0.7500 total= 1.9167
          4      0.8000 total= 2.7167
          5      0.8333 total= 3.5500
          6      0.8571 total= 4.4071
          7      0.8750 total= 5.2821
          8      0.8889 total= 6.1710
          9      0.9000 total= 7.0710
          10      0.9091 total= 7.9801
          11      0.9167 total= 8.8968
          12      0.9231 total= 9.8199
          13      0.9286 total= 10.7484
          14      0.9333 total= 11.6818
          15      0.9375 total= 12.6193
          16      0.9412 total= 13.5604
          17      0.9444 total= 14.5049
          18      0.9474 total= 15.4523
          19      0.9500 total= 16.4023
          20      0.9524 total= 17.3546
          

          【讨论】:

          • 如果改成for循环,就不需要count
          • 谢谢,已重构。
          猜你喜欢
          • 2018-09-02
          • 1970-01-01
          • 2013-10-24
          • 2021-12-31
          • 1970-01-01
          • 2013-10-31
          • 1970-01-01
          • 2020-03-11
          • 1970-01-01
          相关资源
          最近更新 更多