简体   繁体   English

如何在自身内部引用列表的元素?

[英]How to reference an element of a list inside itself?

How would I go about making reference to an element from a list inside that list? 我将如何去引用列表中列表中的元素? For example, 例如,

settings = ["Exposure", "0", random_time(settings[0])]

Where the third element makes reference to the first. 第三个元素引用第一个元素的地方。 I could verbosely state "Exposure" but I am trying to set it up so that even if the first element is changed the third changes with it. 我可以详细说明“曝光”,但是我试图将其设置为即使第一个元素被更改,第三个元素也随之更改。

Edit: I think maybe my question wasn't clear enough. 编辑:我想也许我的问题还不够清楚。 There will be more than one setting each using the generic function "random_time", hence the need to pass the keyword of the setting. 每个使用通用函数“ random_time”的设置将不止一个,因此需要传递设置的关键字。 The reference to the first element is so I only have to make modifications to the code in one place. 对第一个元素的引用是,所以我只需要在一个地方对代码进行修改。 This value will not change once the script is running. 脚本运行后,此值将不会更改。

I will try and use a list of keywords that the settings list makes reference to. 我将尝试使用设置列表引用的关键字列表。

The right-hand expression is evaluated first , so when you evaluate 右边的表达式首先被求值 ,所以当您求值时

["Exposure", "0", random_time(settings[0])]

the variable settings is not defined yet. 变量settings尚未定义。

A little example: 一个小例子:

a = 1 + 2

First 1 + 2 is evaluated and the result is 3 , after it's evaluated, then the assignment is done: 首先评估1 + 2 ,结果是3 ,评估之后,然后完成分配:

a = 3

One way you could handle this is storing the "changing" string to a variable: 处理此问题的一种方法是将“更改中的”字符串存储到变量中:

var1 = "Exposure"
settings = [var1 , "0", random_time(var1)]

this will work in the list definition, but if, after declaring the list settings , you change var1 , it won't change its third element. 这将在列表定义中起作用,但是如果在声明列表settings之后更改var1 ,它将不会更改其第三个元素。 If you want this to happen, you can try implementing a class Settings , which will be a lot more flexible. 如果您希望发生这种情况,可以尝试实现一个类Settings ,它将更加灵活。

AFAIK you can't. AFAIK,你做不到。 This is common to most programming languages because when you're running your function there the item hasn't been completely created yet. 这在大多数编程语言中很常见,因为在运行函数时,尚未完全创建该项目。

You can't directly. 你不能直接。

You could have both refer to something else, though, and use an attribute of that. 但是,您可能都引用了其他内容,并使用了该属性。

class SettingObj:
    name = "Exposure"

settings = [SettingObj, "0", random_time(SettingObj)]

Now, change the way you work with your settings list so that you look for your name attribute for 1st and 3rd items on the list. 现在,更改settings列表的使用方式,以便在列表中的第一项和第三项中查找name属性。

As others have told you, the syntax you've chosen will try to reference settings before it is created, and therefore it will not work (unless settings already exists because another object was assigned to it on a previous line). 正如其他人告诉您的那样,您选择的语法将在创建之前尝试引用设置,因此它将不起作用(除非设置已经存在,因为在上一行中已为其分配了另一个对象)。

More importantly, in Python, assigning a string to two places will not make it so that changing it in one place will change it in the other. 更重要的是,在Python中,将字符串分配到两个位置将不会使它成为一个字符串,因此在一个位置更改它会在另一个位置更改它。 This applies to all forms of binding, including variable names, lists and object attributes. 这适用于所有形式的绑定,包括变量名,列表和对象属性。

Strings are immutable in Python -- they cannot be changed, only rebinded. 字符串在Python中是不可变的-不能更改,只能重新绑定。 And rebinding only affects a single name (or list position or etc.) at a time. 并且重新绑定一次只影响一个名称(或列表位置等)。 This is different from, say, C, where two names can contain pointers that reference the same spot in memory, and you can edit that spot in memory and affect both places. 这与C语言不同,在C语言中,两个名称可以包含引用内存中同一位置的指针,并且您可以在内存中编辑该位置并影响两个位置。

If you really need to do this, you can wrap the string in an object (custom class, presumably). 如果确实需要执行此操作,则可以将字符串包装在一个对象(可能是自定义类)中。 You could even make the object's interface look like a string in all respects, except that it's not a string primitive but an object with an attribute (say contents ) that's bound to a string. 您甚至可以使对象的界面在所有方面看起来都像字符串,除了它不是字符串原语,而是具有绑定到字符串的具有属性(例如contents )的对象。 Then when you want to change the string, you rebind the object's attribute (that is, obj.contents or whatever). 然后,当您想更改字符串时,可以重新绑定对象的属性(即obj.contents或其他内容)。 Since you are not reassigning the names bound to the object itself, but only a name inside the object, it will change in both places. 由于您不是在重新分配绑定到对象本身的名称,而是仅重新分配对象内部的名称,因此它将在两个地方都更改。

In this particular case you don't just have the same string in both places but you actually have a string in the first position but the result of a function performed on the string in the third position. 在这种特殊情况下,您不仅在两个位置都具有相同的字符串,而且实际上在第一个位置具有一个字符串,但在第三个位置对该字符串执行了函数的结果。 So even if you use an object wrapper, it won't work the way you seem to want it to, because the function needs to be re-run every time. 因此,即使您使用对象包装器,它也不会像您希望的那样工作,因为该函数需要每次重新运行。

There are ways to design your program so that this is not a problem, but without knowing more about your ultimate goal I can't say what they are. 有一些方法可以设计程序,这样就不会有问题,但是如果不了解您的最终目标,我就无法说出它们是什么。

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

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