python 3:列表不改变它们的值 [英] python 3: lists dont change their values

查看:85
本文介绍了python 3:列表不改变它们的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试使用for循环以随机百分比更改一堆列表项.

So I am trying to change a bunch of list items by a random percentage using a for loop.

import random as rdm
list = [1000, 100, 50, 25]
def change():
    for item in list:
        item = item + item*rdm.uniform(-10, 10)
change()
print(list)

(同样我也不知道如何粘贴多行代码,所以我一个接一个地完成了它们,也将非常感谢您的帮助) 现在,当它打印列表时,它仅包含其开头的数字.

(also I dont get how to paste multiple lines of code so I did them one by one, would appreciate help with that too) And now when it prints the list it only consists of the numbers it started with.

推荐答案

您的item = ....行(按它的样子)只是将一个新对象与函数名称空间中的名称item关联.没有理由为什么此操作会更改从中提取item上一个值的列表对象的内容.

Your item =.... line, as it stands, just associates a new object with the name item in the function's namespace. There is no reason why this operation would change the content of the list object from which the previous value of item was extracted.

这是一个可以就地更改列表的列表:

Here is a listing that changes a list in-place:

import random
lyst = [1000,100,50,25]
def change(lyst):
    for i, item in enumerate(lyst):
        item = item + item * random.uniform(-10, 10)
        lyst[i] = item

print(lyst)
change(lyst)
print(lyst)

lyst[i] = ...分配是实际上更改列表内容的关键行.当然,如果需要,您可以将两个分配折叠为一行:lyst[i] = item = .....或者,如果您不想在循环中再次使用它,则可以省略对item的重新分配: ...

The lyst[i] =... assignment is the key line that actually changes the list's content. Of course you can collapse the two assignments into one line if you want: lyst[i] = item =..... Or you can omit the reassignment to item if you're not going to use it again in the loop: lyst[i] = item + item *...

请注意,我还执行了两个较小的修复程序:我将变量名称从list更改为lyst,以便它不会掩盖您对list类的内置引用.我还更改了您的函数,以使其将列表对象作为参数,而不是依靠使用硬编码的全局变量名称来引用它.两者都是很好的做法.与您的问题无关.

Note that I performed two minor fixes in addition: I changed the variable name from list to lyst so that it doesn't overshadow your builtin reference to the list class. I have also altered your function so that it takes the list object as an argument, rather than relying on referring to it using a hard-coded global variable name. Both of these are just good practice; nothing to do with your problem.

最后,请注意,您可以使用所谓的列表理解.如果您不必就地修改列表,即可以以原始列表的修改后的副本结尾,则可以:

Finally, note that you can do all of this much more succinctly and transparently with a so-called list comprehension. If you don't have to modify the list in-place, i.e. if it's OK to end up with a modified copy of the original list:

lyst = [ item + item * random.uniform(-10, 10)  for item in lyst ]

如果您需要就地修改列表(例如,如果其他地方有对原始列表的其他引用,并且它们也应在调用change()之后指向更新的内容),则可以按照Padraic的建议进行操作.评论:

If you need to modify the list in-place (e.g. if other references to the original list exist elsewhere and they, too, should point to the updated content after change() is called) then you can follow the suggestion in Padraic's comment:

lyst[:] = [ item + item * random.uniform(-10, 10)  for item in lyst ]

在最后一种情况下,如果更改括号形状并因此使用

In that last case, you can even save memory (which will only be a concern for very large lists) if you change the bracket shape and thereby use a generator expression instead:

lyst[:] = ( item + item * random.uniform(-10, 10)  for item in lyst )

这篇关于python 3:列表不改变它们的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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