简体   繁体   English

解包参数时修改列表

[英]Modifying a list when unpacking parameters

I've recently come accross a problem where I needed to find an elegant way to itterate through a list while having readable code.我最近遇到了一个问题,我需要找到一种优雅的方式来遍历列表,同时拥有可读的代码。 I discovered the 'unpacking'.我发现了“拆包”。

Assume I have a list : l = [ ['param_1', 1] ['param_2',2] ]假设我有一个列表: l = [ ['param_1', 1] ['param_2',2] ]

I used to iterate through this list as follows :我曾经按如下方式遍历此列表:

for param in l:
  if param[0] == 'param_1':
    param[1] = 27

Now I am doing :现在我正在做:

for param_name, param_value in l:
  if param_name == 'param_1':
    param_value = 27

Though it is much more readable, the fact that I assign param_value to 27 doesn't change my list as it used to with the old method.虽然它更具可读性,但我将 param_value 分配给 27 的事实并没有像以前使用旧方法那样改变我的列表。 Any idea why ?知道为什么吗? Couldn't find some answer on the internet.在互联网上找不到一些答案。

This is what dictionaries are for;这就是字典的用途; you're trying to update the value associated with the string param_1 .您正在尝试更新与字符串param_1关联的值。

l = [ ['param_1', 1], ['param_2',2] ]
d = dict(l)
# {'param_1': 1, 'param_2': 2}

Instead of iterating over the whole list, you can now set the value directly:您现在可以直接设置值,而不是遍历整个列表:

d['param_1'] = 27
# {'param_1': 27, 'param_2': 2}

Note that this will only work properly if your parameter names are all unique;请注意,这只有在您的参数名称都是唯一的情况下才能正常工作; identical names will all get collapsed into the same dictionary key.相同的名称将全部折叠到同一个字典键中。

You said "Couldn't find some answer on the internet" , the answer is in between the lines, indeed.您说“在互联网上找不到答案” ,答案确实在字里行间。 In the official documentation, though, the Data Model has all the basic stones to understand what's going on.但是,在官方文档中,数据模型具有了解正在发生的事情的所有基本要素。 This is related to objects' mutability and variables name binding .这与对象的可变性和变量名绑定有关。

In the first case,在第一种情况下,

for param in l:
    ...

, param is a list. , param是一个列表。 Lists are mutable objects, containers which values can change.列表是可变对象,是值可以改变的容器。

When you change one of param 's values the way you do,当您以您的方式更改param的值之一时,

    param[1] = 27

, you're telling Python to modify the second value of the list param is pointing to. ,您是在告诉 Python 修改param指向的列表的第二个值。

On the other hand, in the second case,另一方面,在第二种情况下,

for param_name, param_value in l:
    ...

, you're unpacking those lists inside l into the corresponding pair of (scalar) values, param_name and param_value . ,您将l内的这些列表解压缩为相应的(标量)值对param_nameparam_value The values they are pointing to are immutable types (string, number), and you also "lost" the pointer to the containet/list they were in (ie, param in the first case).它们指向的值是不可变类型(字符串、数字),并且您还“丢失”了指向它们所在的容器/列表的指针(即第一种情况下的param )。

Now, when you do param_value = 27 , Python is actually creating a new space in memory to allocate 27 and param_value is now referring to that value, but this is a different variable -- same name param_value but different space in memory.现在,当您执行param_value = 27时,Python 实际上是在内存中创建一个新空间来分配27 ,而param_value现在指的是该值,但这是一个不同的变量——同名param_value但内存中的空间不同。


References:参考:

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

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