如何将列表元素作为参考传递? [英] How to pass a list element as reference?

查看:45
本文介绍了如何将列表元素作为参考传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将列表的单个元素传递给函数.我想修改那个元素,也就是列表本身.

def ModList(element):元素 = '两个'l = 列表();l.append('一个')l.append('两个')l.append('三')打印 lModList(l[1])打印 l

但是这个方法不会修改列表.这就像元素是按值传递的.输出为:

['一','二','三']['一二三']

我希望函数调用后列表的第二个元素是TWO":

['一','二','三']

这可能吗?

解决方案

这里的解释是正确的.但是,由于我想以类似的方式滥用 python,因此我将提交此方法作为解决方法.

从列表中调用特定元素直接返回列表中该元素的值的副本.即使复制列表的子列表也会返回对包含值副本的数组的新引用.考虑这个例子:

<预><代码>>>>a = [1, 2, 3, 4]>>>b = a[2]>>>乙3>>>c = a[2:3]>>>C[3]>>>b=5>>>c[0]=6>>>一种[1, 2, 3, 4]

b(仅值复制)和 c(从 a 复制的子列表)都不能更改 中的值一个.尽管它们的起源相同,但没有链接.

然而,numpy 数组使用原始"内存分配并允许返回数据视图.视图允许以不同的方式表示数据,同时保持与原始数据的关联.因此,一个工作示例是

<预><代码>>>>将 numpy 导入为 np>>>a = np.array([1, 2, 3, 4])>>>一种数组([1, 2, 3, 4])>>>b = a[2]>>>乙3>>>b=5>>>一种数组([1, 2, 3, 4])>>>c = a[2:3]>>>C数组([3])>>>c[0]=6>>>一种数组([1, 2, 6, 4])>>>

虽然提取单个元素仍然只按值复制,但维护元素2的数组视图是引用到a的原始元素2> (虽然现在是 c0 元素),对 c 的值所做的改变改变了 a 也是.

Numpy ndarrays 有许多不同的类型,包括通用对象类型.这意味着您可以为几乎任何类型的数据维护这种按引用"行为,而不仅仅是数值.

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':

['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.

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. 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. 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.

Numpy ndarrays have many different types, including a generic object type. This means that you can maintain this "by-reference" behavior for almost any type of data, not only numerical values.

这篇关于如何将列表元素作为参考传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆