简体   繁体   English

Python引用另一个列表中列表中的项

[英]Python referring to an item in a list from another list

I have a list of variables (listA) from from another list (listB). 我有一个来自另一个列表(listB)的变量列表(listA)。 The problem I am having is that the items from listB are being passed by value to listA rather than by reference. 我遇到的问题是listB中的项目是通过值传递给listA而不是通过引用。 Is there anyway I can access the the object in listB after having put its value in listA? 无论如何,在将其值放入listA后,我可以访问listB中的对象吗?

For example: 例如:

listB = [1,2,3,4,5]
listA = [listB[0], listB[1]]
listA[0] = 0

this makes listA equal to [0, 2], and leaves listB unchanged. 这使listA等于[0,2],并使listB保持不变。 I would like to modify listB so that it becomes [0,2,3,4,5]. 我想修改listB,使其成为[0,2,3,4,5]。

I have of course come up with a solution to this, but its ugly, and I was wondering if there was an elegant way of doing this. 我当然想出了一个解决方案,但它的丑陋,我想知道是否有一种优雅的方式来做到这一点。

Everything in python is a reference. python中的所有内容都是一个参考。 After all those statements are executed, listB[1] and listA[1] are literally the same object. 执行listB[1]所有这些语句后, listB[1]listA[1] 实际上是同一个对象。 (you can check, by calling id(listB[1]) and id(listA[1]) . (您可以通过调用id(listB[1])id(listA[1])

The reason listA[0] and listB[0] are different is merely because you put a different reference into that spot. listA[0]listB[0]不同的原因仅仅是因为你将不同的引用放入该位置。

Judging from your description, you don't want to a listA that stores references to the objects in listB . 从您的描述来看,您不希望listA存储对listA中对象的listB What you want is a listA that is a view of listB . 你需要的是一个listA景色 listB I believe you have only two options: 我相信你只有两个选择:

  • Create a special sequence that internally stores a reference to listA , and whose __getitem__ and __setitem__ methods perform lookups into listA when invoked. 创建一个特殊的序列,在内部存储对listA的引用,其__getitem____setitem__方法在调用时执行查找到listA

  • Create special a special reference types that contains something like a "sequence and index". 创建特殊的特殊引用类型,其中包含“序列和索引”之类的内容。 Put these references into listA . 将这些引用放入listA But, to modify listB through listA , you'll have to invoke some sort of "get" and "set" members of these reference objects. 但是,要通过listA修改listB ,您必须调用这些引用对象的某种“get”和“set”成员。

You can't. 你不能。 First of all integers are immutable and they don't work like integers in C/C++. 首先,整数是不可变的,它们不像C / C ++中的整数那样工作。 You can't get pointer/reference to an integer and then change it (I mean you can, you always have a reference, but it is usually reference to a single object; check this x = 1; y = 1; print id(x), id(y) ; id values should be the same and they are memory addresses). 你不能获得一个整数的指针/引用然后改变它(我的意思是你可以,你总是有一个引用,但它通常引用一个对象;检查这个x = 1; y = 1; print id(x), id(y) ; id值应该相同,它们是内存地址)。 What you can do is get index of elements in listB and change the list, eg: 你可以做的是获取listB中的元素索引并更改列表,例如:

listA = [0, 1]
listB[listA[0]] = 0

But probably you are trying to do something you are not supposed to do, because Python works differently than C++. 但是你可能正在尝试做一些你不应该做的事情,因为Python的工作方式与C ++不同。 What are you trying to achieve? 你想要实现什么目标?

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

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