Python 获取指向列表元素的指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14905527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:48:44  来源:igfitidea点击:

Get a pointer to a list element

pythonlistpointers

提问by Apos

I was wondering if it was possible to get a "pointer" to an element in a python list. That way, I would be able to access my element directly without needing to know my element's index. What I mean by that is that in a list, you can add elements anywhere; at the start, in the middle or even at the end, yet the individual elements aren't moved from their actual memory location. In theory, it should be possible to do something like:

我想知道是否有可能获得指向 python 列表中元素的“指针”。这样,我就可以直接访问我的元素,而无需知道我的元素的索引。我的意思是,在列表中,您可以在任何地方添加元素;在开始、中间甚至结束时,单个元素并没有从它们的实际内存位置移动。从理论上讲,应该可以执行以下操作:

myList = [1]

myList = [1]

[1]

[1]

element = &myList[0]

element = &myList[0]

element would act as a pointer here.

element 将在这里充当指针。

myList.insert(0, 0)
myList.append(2)

[0, 1, 2]

[0, 1, 2]

At this point, I would still be able to access the element directly even though it's index within the list has changed.

此时,即使列表中的索引已更改,我仍然可以直接访问该元素。

The reason I want to do this is because in my program, it would be way too tedious to keep track of every item I add to my list. Each item is generated by an object. Once in a while, the object has to update the value, yet it can't be guaranteed that it will find its item at the same index as when it was added. Having a pointer would solve the problem. I hope that makes sense.

我想这样做的原因是因为在我的程序中,跟踪我添加到列表中的每个项目都太乏味了。每个项目都由一个对象生成。有时,对象必须更新值,但不能保证它会在与添加时相同的索引处找到它的项目。有一个指针可以解决问题。我希望这是有道理的。

What would be the right way to do something like that in Python?

在 Python 中做这样的事情的正确方法是什么?

采纳答案by asermax

There's no concept of pointers on python (at least that I'm aware of).

python 上没有指针的概念(至少我知道)。

In case you are saving objects inside your list, you can simply keep a reference to that object.

如果您将对象保存在列表中,您可以简单地保留对该对象的引用。

In the case you are saving primitive values into your list, the approach I would take is to make a wrapper object around the value/values and keep a reference of that object to use it later without having to access the list. This way your wrapper is working as a mutable object and can be modified no matter from where you are accesing it.

如果您将原始值保存到列表中,我将采用的方法是围绕值创建一个包装对象,并保留该对象的引用,以便稍后使用它而无需访问列表。通过这种方式,您的包装器作为可变对象工作,并且无论您从何处访问它都可以对其进行修改。

An example:

一个例子:

class FooWrapper(object):
    def __init__(self, value):
         self.value = value

# save an object into a list
l = []
obj = FooWrapper(5)
l.append(obj)

# add another object, so the initial object is shifted
l.insert(0, FooWrapper(1))

# change the value of the initial object
obj.value = 3
print l[1].value # prints 3 since it's still the same reference

回答by jfs

element = mylist[0]already works if you don't need to change the element or if elementis a mutable object.

element = mylist[0]如果您不需要更改元素或者element是可变对象,则已经可以使用。

Immutable objects such as intobjects in Python you can not change. Moreover, you can refer to the same object using multiple names in Python e.g., sys.getrefcount(1)is ~2000 in a fresh REPL on my system. Naturally, you don't want 1to mean 2all of a sudden in all these places.

不可变对象,例如intPython 中的对象,您无法更改。此外,您可以在 Python 中使用多个名称来引用同一个对象,例如,sys.getrefcount(1)在我系统上的新 REPL 中是 ~2000。当然,你不想1意味着2一下子在所有这些地方。

If you want to change an object later then it should be mutable e.g., if mylist[0] == [1]then to change the value, you could set element[0] = 2. A custom object instead of the [1]list could be more appropriate for a specific application.

如果您想稍后更改对象,那么它应该是可变的,例如,如果mylist[0] == [1]要更改值,您可以设置element[0] = 2. 自定义对象而不是 [1]列表可能更适合特定应用程序。

As an alternative, you could use a dictionary (or other namespace objects such as types.SimpleNamespace) instead of the mylistlist. Then to change the item, reference it by its name: mydict["a"] = 2.

作为替代方案,您可以使用字典(或其他命名空间对象,例如types.SimpleNamespace)而不是mylist列表。然后要更改项目,请通过其名称引用它:mydict["a"] = 2