【问题标题】:How can you update a Python function parameter each time it's called in a loop?每次在循环中调用 Python 函数参数时,如何更新它?
【发布时间】:2021-01-19 08:41:56
【问题描述】:

我在 Jupyter 中编写了一个函数,它接收一个包含帕斯卡三角形一行的值的列表,然后生成下一行。由于项目的要求,我需要从一个只包含整数 1 的列表开始,然后创建一个调用该函数 5 次的循环(留下总共 6 行)。

import math

def pascals_triangle(list):
    
    nextrow = []
    numrows = len(list)
    numcols = len(list)
   
    count = 0
 
    for x in range(numcols+1):
        val = ((math.factorial(numrows)) / ((math.factorial(count)) * math.factorial(numrows - count)))
        nextrow.append(val)
        count += 1
    
    numrows += 1
    numcols += 1
    print(nextrow)
    
    return nextrow
list = [1]

print(list)

for x in range(5):
    pascals_triangle(list)

这是我的问题:我无法以打印出所有 6 个递增行的方式更新我的函数参数或计数。现在,它只打印五次相同的返回值。

预期输出:

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]

实际输出:

[1]
[1, 1]
[1, 1]
[1, 1]
[1, 1]
[1, 1]

如何让函数在每次调用时使用之前的返回值,或者让我的 numrows/numcols 为下一次函数调用递增?

【问题讨论】:

  • 旁注:您应该避免使用内置函数的名称(list、dict、id、...)作为变量名。虽然 Python 确实允许这样做,但您实际上是在覆盖内置函数,这可能会在后续代码中咬到您。
  • 你一遍又一遍地传递相同的值。你为什么指望它是昌河?
  • 感谢大家的反馈!这实际上是我第一次用 Python 编写任何东西,而且我是一个整体编程新手,所以我感谢你的耐心。

标签: python loops for-loop parameter-passing jupyter


【解决方案1】:

首先,list 是 Python 原生列表对象的关键字。你永远不应该将它用作变量名,因为它会导致以后无法在代码中调用列表构造函数、进行类型比较,并且通常会导致意外行为

除此之外,您缺少的是将pascals_triangle 的输出重新分配给循环的下一次迭代中的函数输入。有了这么小的变化,它应该可以工作

我还将 print 语句移到您的函数之外,这样您就可以更好地控制打印的内容和时间。

import math


def pascals_triangle(lst):
    nextrow = []
    # numrows and numcols will be equivalent for all cases,
    # and increase for each call of the function.
    numrows = len(lst)
    numcols = len(lst)

    # count is the column we are currently looking at and will go from 1 to numcols.
    count = 0

    for x in range(numcols + 1):
        val = ((math.factorial(numrows)) / ((math.factorial(count)) * math.factorial(numrows - count)))
        nextrow.append(val)
        count += 1

    numrows += 1
    numcols += 1

    return nextrow


cur_row = [1]
for _ in range(5):
    cur_row = pascals_triangle(cur_row)
    print(cur_row)

【讨论】:

    【解决方案2】:
    import math
    
    def pascals_triangle(list):
        
        nextrow = []
        # numrows and numcols will be equivalent for all cases, 
        # and increase for each call of the function.
        numrows = len(list)
        numcols = len(list)
       
        # count is the column we are currently looking at and will go from 1 to numcols.
        count = 0
     
        for x in range(numcols+1):
            val = ((math.factorial(numrows)) / ((math.factorial(count)) * math.factorial(numrows - count)))
            nextrow.append(int(val))
            count += 1
        
        numrows += 1
        numcols += 1
        print(nextrow)
        
        return nextrow
    
    list = [1]
    
    print(list)
    
    for x in range(5):
        list = pascals_triangle(list)
    

    你几乎拥有它。这应该可以工作,将“list”变量设置为函数返回的输出。

    注意:“list”是一个默认函数,不应真正被覆盖。我建议使用另一个变量名,以免以后造成任何令人困惑的错误。

    【讨论】:

    • 是的,请尽快选择其他名称。
    猜你喜欢
    • 2021-02-15
    • 2014-09-27
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多