简体   繁体   English

如何将列表元素作为参考传递?

[英]How to pass a list element as reference?

I am passing a single element of a list to a function.我将列表的单个元素传递给函数。 I want to modify that element, and therefore, the list itself.我想修改那个元素,也就是列表本身。

def ModList(element):
    element = 'TWO'

l = list();
l.append('one')
l.append('two')
l.append('three')
print l
ModList(l[1])
print l

But this method does not modify the list.但是这种方法不会修改列表。 It's like the element is passed by value.这就像元素是按值传递的。 The output is:输出是:

['one','two','three']
['one','two','three']

I want that the second element of the list after the function call to be 'TWO':我希望函数调用后列表的第二个元素是“TWO”:

['one','TWO','three']

Is this possible?这可能吗?

The explanations already here are correct.已经在这里的解释是正确的。 However, since I have wanted to abuse python in a similar fashion, I will submit this method as a workaround.但是,由于我想以类似的方式滥用 python,因此我将提交此方法作为解决方法。

Calling a specific element from a list directly returns a copy of the value at that element in the list.从列表中调用特定元素直接返回列表中该元素的值的副本。 Even copying a sublist of a list returns a new reference to an array containing copies of the values.即使复制列表的子列表也会返回对包含值副本的数组的新引用。 Consider this example:考虑这个例子:

>>> a = [1, 2, 3, 4]
>>> b = a[2]
>>> b
3
>>> c = a[2:3]
>>> c
[3]
>>> b=5
>>> c[0]=6
>>> a
[1, 2, 3, 4]

Neither b , a value only copy, nor c , a sublist copied from a , is able to change values in a . b仅复制值, c也不是从a复制的子列表,都不能更改a值。 There is no link, despite their common origin.尽管它们有共同的起源,但没有任何联系。

However, numpy arrays use a "raw-er" memory allocation and allow views of data to be returned.但是,numpy 数组使用“原始”内存分配并允许返回数据视图。 A view allows data to be represented in a different way while maintaining the association with the original data.视图允许以不同的方式表示数据,同时保持与原始数据的关联。 A working example is therefore因此,一个工作示例是

>>> import numpy as np
>>> a = np.array([1, 2, 3, 4])
>>> a
array([1, 2, 3, 4])
>>> b = a[2]
>>> b
3
>>> b=5
>>> a
array([1, 2, 3, 4])
>>> c = a[2:3]
>>> c
array([3])
>>> c[0]=6
>>> a
array([1, 2, 6, 4])
>>> 

While extracting a single element still copies by value only, maintaining an array view of element 2 is referenced to the original element 2 of a (although it is now element 0 of c ), and the change made to c 's value changes a as well.虽然仅通过值提取单个元件仍然份,保持元件的阵列视图2为参考原始元件2a (虽然现在元素0c ),并进行变更c的值改变a作为好。

Numpy ndarray s have many different types, including a generic object type. Numpy ndarray有许多不同的类型,包括通用对象类型。 This means that you can maintain this "by-reference" behavior for almost any type of data, not only numerical values.这意味着您可以为几乎任何类型的数据维护这种“按引用”行为,而不仅仅是数值。

Python is a pass by value language hence you can't change the value by assignment in the function ModList . Python 是一种按值传递的语言,因此您不能通过函数ModList赋值来更改值。 What you could do instead though is pass the list and index into ModList and then modify the element that way您可以做的是将列表和索引传递给ModList ,然后以这种方式修改元素

def ModList(theList, theIndex) :
  theList[theIndex] = 'TWO'

ModList(l, 1)

Python doesn't do pass by reference . Python 不会通过引用传递 Just do it explicitly:只需明确地做到这一点:

l[1] = ModList(l[1])

Also, since this only changes one element, I'd suggest that ModList is a confusing name.此外,由于这只会更改一个元素,因此我建议ModList是一个令人困惑的名称。

In many cases you can also consider to let the function both modify and return the modified list.在很多情况下,你也可以考虑让函数既修改又返回修改后的列表。 This makes the caller code more readable:这使得调用者代码更具可读性:

def ModList(theList, theIndex) :
          theList[theIndex] = 'TWO'
    return theList

l = ModList(l, 1)

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

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