Python 在函数的参数中指定列表的长度

Python 在函数的参数中指定列表的长度,python,function,parameters,arguments,Python,Function,Parameters,Arguments,因此,我想将列表的长度设置为函数中的变量,但它表示我的列表未定义: def maxElement(list, start=0, end = len(serie)): return max(serie[start:end]) serie = [9, 3, 6, 1, 7, 5, 4, 8, 2] print maxElement(serie) 在定义函数时,serie尚未定义。此外,默认参数是在模块加载时计算和绑定的,而不是在调用函数时。因此,您不能引用lamaxElement(lst

因此,我想将列表的长度设置为函数中的变量,但它表示我的列表未定义:

def maxElement(list, start=0, end = len(serie)):
    return max(serie[start:end])

serie = [9, 3, 6, 1, 7, 5, 4, 8, 2]
print maxElement(serie)

在定义函数时,
serie
尚未定义。此外,默认参数是在模块加载时计算和绑定的,而不是在调用函数时。因此,您不能引用la
maxElement(lst,start=0,end=len(lst))
或任何其他依赖于动态运行时的默认值:

def maxElement(lst, start=0, end=None):  # don't shadow built-in name list
    if end is None:
        end = len(lst)
    return max(lst[start:end])  # do not use serie here, but lst

> serie = [9, 3, 6, 1, 7, 5, 4, 8, 2]
> print maxElement(serie)
9

> print maxElement(serie, start=1, end=6)
7

在定义函数时,
serie
尚未定义。此外,默认参数是在模块加载时计算和绑定的,而不是在调用函数时。因此,您不能引用la
maxElement(lst,start=0,end=len(lst))
或任何其他依赖于动态运行时的默认值:

def maxElement(lst, start=0, end=None):  # don't shadow built-in name list
    if end is None:
        end = len(lst)
    return max(lst[start:end])  # do not use serie here, but lst

> serie = [9, 3, 6, 1, 7, 5, 4, 8, 2]
> print maxElement(serie)
9

> print maxElement(serie, start=1, end=6)
7

您的代码有几个问题

首先,您实际上没有使用函数的
list
参数。相反,您使用的是全局列表
serie
。顺便说一句,您不应该使用
list
作为变量名,因为它会隐藏(覆盖)内置的
list
类型,这可能会导致神秘的bug

正如Schwobasegll提到的,默认函数参数是在定义函数时计算的,而不是在调用函数时计算的。因此,当您执行
end=len(serie)
时,会在定义函数时将
end
设置为
serie
的当前长度。但是,您的脚本在定义了
maxElement
之后定义了
serie
,因此尚未定义该名称,这就是为什么会出现以下错误:

NameError: name 'serie' is not defined
如果将
serie
的定义放在
maxElement
的定义之前,则可以消除该错误,但由于前面提到的错误,该函数仍然无法正常工作

实际上,可能还有另一个错误。看起来您希望搜索
开始
结束
之间的最大元素,包括。如果是这样,您需要使用类似于
lst[start:end+1]
。如果您想使用正常的Python约定,其中
end
被排除在范围之外,那么您可以使用
lst[start:end]
。请注意,切片的结束索引是否超出列表并不重要;对于
n
>0,
lst[start:len(lst)+n]
lst[start:len(lst)]
完全相同

这是您的代码的修复版本。我稍微修改了数据,使其更易于测试

def max_element(lst, start=0, end=None):
    if end is None:
        end = len(lst)
    return max(lst[start:end + 1])

serie = [0, 3, 6, 1, 7, 5, 4, 8, 2]

print serie
print max_element(serie)
print max_element(serie, start=4)
print max_element(serie, end=2)
print max_element(serie, start=4, end=6)
print max_element(serie, start=3, end=3)
输出

[0, 3, 6, 1, 7, 5, 4, 8, 2]
8
8
6
7
1
然而,该功能实际上并不必要。您应该直接在列表中调用
max
,必要时进行切片,例如:

print max(serie[4:7])

您的代码有几个问题

首先,您实际上没有使用函数的
list
参数。相反,您使用的是全局列表
serie
。顺便说一句,您不应该使用
list
作为变量名,因为它会隐藏(覆盖)内置的
list
类型,这可能会导致神秘的bug

正如Schwobasegll提到的,默认函数参数是在定义函数时计算的,而不是在调用函数时计算的。因此,当您执行
end=len(serie)
时,会在定义函数时将
end
设置为
serie
的当前长度。但是,您的脚本在定义了
maxElement
之后定义了
serie
,因此尚未定义该名称,这就是为什么会出现以下错误:

NameError: name 'serie' is not defined
如果将
serie
的定义放在
maxElement
的定义之前,则可以消除该错误,但由于前面提到的错误,该函数仍然无法正常工作

实际上,可能还有另一个错误。看起来您希望搜索
开始
结束
之间的最大元素,包括。如果是这样,您需要使用类似于
lst[start:end+1]
。如果您想使用正常的Python约定,其中
end
被排除在范围之外,那么您可以使用
lst[start:end]
。请注意,切片的结束索引是否超出列表并不重要;对于
n
>0,
lst[start:len(lst)+n]
lst[start:len(lst)]
完全相同

这是您的代码的修复版本。我稍微修改了数据,使其更易于测试

def max_element(lst, start=0, end=None):
    if end is None:
        end = len(lst)
    return max(lst[start:end + 1])

serie = [0, 3, 6, 1, 7, 5, 4, 8, 2]

print serie
print max_element(serie)
print max_element(serie, start=4)
print max_element(serie, end=2)
print max_element(serie, start=4, end=6)
print max_element(serie, start=3, end=3)
输出

[0, 3, 6, 1, 7, 5, 4, 8, 2]
8
8
6
7
1
然而,该功能实际上并不必要。您应该直接在列表中调用
max
,必要时进行切片,例如:

print max(serie[4:7])

调用
maxElement
签名中的
len
函数将失败,因为尚未定义
serie
。但更好的问题是,你为什么要这么做?您的列表正在作为参数传递,因此在函数中调用
len(list)
以了解其长度。顺便说一句,不要将
list
用作变量名,因为这会影响内置的
list
类型。在
maxElement
签名中调用
len
函数将失败,因为
系列
尚未定义。但更好的问题是,你为什么要这么做?您的列表将作为参数传递,因此在函数中调用
len(list)
以了解其长度。顺便说一句,不要使用
list
作为变量名,因为这会影响内置的
list
类型。非常全面的解释!但是为什么要在片中使用
end+1
,而不是只使用
end
?更新:毕竟,最后一个列表索引是
len(lst)-1
无论如何……非常全面的解释!但是为什么要在片中使用
end+1
,而不是只使用
end
?更新:毕竟,最后一个列表索引是
len(lst)-1
。。