繁体   English   中英

将列表传递给 Python 函数

[英]Pass a List into Python Function

作为一名新的 Python 程序员,我有两个关于 list 的问题,非常感谢您的建议:

问题 1:

对于以下代码:

nums1 = [1,2,3,8,0,0,0]
m = 3
nums2 = [2,5,6]
n = 3
def merge(nums1, m, nums2, n):
    nums1[:] = sorted(nums1[:m]+nums2)
merge(nums1, m, nums2, n)
nums1

它的作用是:将列表 nums1 和列表 nums2 传递到合并函数中,并将它们与列表 nums1 合并到列表 nums1 中,其中 nums1 中的前 m 项和 nums2 中的 n 项,并对列表 nums1 进行排序。 所以结果是: [1, 2, 2, 3, 5, 6] 所以我的问题是:既然列表 nums1 是在函数合并的范围之外定义的,它为什么有更新 nums1 的能力? 在以下示例中:

x = 10
def reassign(x):
    x = 2
reassign(x)
x

变量 x 是在函数 reassign 之外定义的,而 reassign 函数无法更新在 reassign 之外定义的 x,这就是 x 返回 10 的原因。

问题2:

在我提供的上面的代码中,如果我像下面这样写:

注意:我只是在分配 sorted(nums1[:m]+nums2) 时将 nums1[:] 修改为 nums1

nums1 = [1,2,3,8,0,0,0]
m = 3
nums2 = [2,5,6]
n = 3
def merge(nums1, m, nums2, n):
    nums1 = sorted(nums1[:m]+nums2)
merge(nums1, m, nums2, n)
nums1

nums1 返回 [1,2,3,8,0,0,0],所以我的问题是:在 nums1 后添加 [:] 后,为什么函数有 nums1 的能力? 在那个例子中 [:] 是什么?

要复制您所说的内容,请执行以下操作:

var = 10
lst = [1, 2, 3]

def func():
    var = 11
    lst[:] = [1, 2, 3, 4]

func()
print(var, lst) 

以上将输出10 [1, 2, 3, 4] 现在注意以下几点:

var = 10
lst = [1, 2, 3]

def func():
    print(var)
    print(lst)

func()

输出10 [1, 2, 3] -- 所以我们知道函数可以访问全局变量,但在大多数情况下不能修改它们。 现在让我们看看这两种情况( intlist ): 两种情况如下:

  1. 由于局部作用域和全局作用域之间的引用不同, var变量没有被修改(虽然我们可以访问全局作用域,但我们不能修改它)。 我建议使用打印globals()locals()来获得乐趣。 如果我们这样做,则可以修复这种情况:
def func():
    global var
    var = 11
  1. 使用[:]表示法修改lst变量,因为正如此处所引用的,切片赋值[:]使用operator函数setitem() 因此,从技术上讲, lst[:] =相当于:
from operator import setitem

lst = [1, 2, 3]

# Both of these are equivalent. 
lst[:] = [1, 2, 3, 4] 
setitem(a, slice(0, len(a)), [1, 2, 3, 4])

setitem不区分本地或全局范围。

(不要使用[:]东西。这太可怕了。)

我的非正式回答

当您说nums1[:] Python 正在查找名为nums1的 GLOBAL 列表。 然而,函数内部,Python 首先关注变量。 为什么?
- 如果您在函数之外的任何变量名称现在被限制用作函数内部的 DIFFERENT 变量,那将会很糟糕

H = True # some variable.pretend it means "High"

def euro_height(inches):
    H = inches # since H is a nice abbrev for height in inches
    return H*2.54 # Centimeters

我不希望函数中的H覆盖我已经存储的内容。 因此,在euro_heightH被视为不同的局部变量,只有该函数才能看到和使用。 如果我想从函数外部使用H ,我必须首先告诉 Python 访问它。 那我就可以用了。

H = True # some variable.pretend it means "High"

def euro_height(inches):
    global H
    print(H) # will say true

    renamed_var = inches # since H is a nice abbrev for height in inches
    return renamed_var*2.54 # Centimeters

如果我现在在函数内部分配H = inches ,它将覆盖全局HTrue值。 因此,我将其重命名,因为已经有一个H我想使用。

所有这些的名称称为namespaces 希望你正在做一个 Python 教程。 当他们教授函数时,你会明白这一点。 如果你不是,我建议你做一个教程。

有关与您发生的事情相关的此答案的更多信息,请查看此处的交互式示例https://www.programiz.com/python-programming/global-local-nonlocal-variables

另外,永远不要再次使用mylist[:] =) 这是糟糕的语法。 它只返回整个列表。 所以只需使用列表mylist的名称。 通过添加括号,您强制查找全局变量,并创建了您的问题

如果您感到困惑,请执行以下操作。

首先安置和改变不是一回事

nums1 = [1,2,3,8,0,0,0]
lst = None
m = 3
nums2 = [2,5,6]
n = 3
def merge(nums1, m, nums2, n):
    global lst 
    lst = sorted(nums1[:m]+nums2)
merge(nums1, m, nums2, n)
print(lst)

nums1[:]表示您的整个列表。 如果您使用nums1[:]而不是nums1您可以使用sorted(nums1[:m]+nums2)更改以前的nums1[:]列表。 当你这样做时,你的前一个和最新的列表nums1改变,所以你在nums1数组列表中用新的附加变量更改 new 。 但是如果你只使用nums1而不是nums1[:]最新的nums1现在指的是一个与前者不同的列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM