为什么不能在没有枚举的循环中分配列表? [英] Why can't you assign to a list in a loop without enumerate?

查看:62
本文介绍了为什么不能在没有枚举的循环中分配列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码片段中,我尝试为列表a中的每个索引分配10并且失败,因为当我尝试将数字分配给10时,数字

是第i个索引的深层副本(这个语句是否正确?)。

In the following code snippet, I attempt to assign 10 to every index in
the list a and fail because when I try to assign number to 10, number
is a deep copy of the ith index (is this statement correct?).


> > a = [1,2,3,4,5]
数字在:
>>a = [1,2,3,4,5]
for number in a:



.. .. number = 10

....

.... number = 10
....


> ;> a
>>a



[1,2,3,4,5]


所以,我不得不求助于使用枚举来分配列表:

[1, 2, 3, 4, 5]

So, I have to resort to using enumerate to assign to the list:


> > for i,枚举中的数字(a):
>>for i, number in enumerate(a):



.... a [i] = 10
....

.... a[i] = 10
....


>> a
>>a



[10,10,10,10,10]


我的问题是,什么是返回

的深层副本的动机,在for循环中的第i个索引处返回值而不是值

本身?另外,有没有办法分配到for循环中的列表(使用

作为上面使用的代码)而不使用枚举?


谢谢,


Danny

[10, 10, 10, 10, 10]

My question is, what was the motivation for returning a deep copy of
the value at the ith index inside a for loop instead of the value
itself? Also, is there any way to assign to a list in a for loop (with
as little code as used above) without using enumerate?

Thanks,

Danny

推荐答案

Danny Colligan写道:
Danny Colligan wrote:

我的问题是,在for循环内的第i个索引处返回一个深度副本的动机是什么,而不是值

本身?
My question is, what was the motivation for returning a deep copy of
the value at the ith index inside a for loop instead of the value
itself?



我不确定单词deep copy和价值真的意味着你认为他们做了什么。也许你应该花一点时间使用Python的

文档,而不是编写关于

可能如何工作的荒谬理论,只是每次现实不同意时都会感到失望。

I''m not sure the words "deep copy" and "value" really means what you
think they do. maybe you should spend a little time with Python''s
documentation, instead of making up absurd theories about how things
might work, only to get disappointed every time reality disagrees.


此外,有没有办法分配到for循环中的列表(

,如上所用的代码少)使用枚举?
Also, is there any way to assign to a list in a for loop (with
as little code as used above) without using enumerate?



使用枚举有什么问题?还是列表理解?或者一些

其他可以用来建立一组

值的列表的其他方法吗?


< ; / F>

what''s wrong with using enumerate? or a list comprehension? or some
other of the many different ways you can use to build a list from a set
of values?

</F>


" Danny Colligan" < da *********** @ gmail.comwrote:
"Danny Colligan" <da***********@gmail.comwrote:

在下面的代码片段中,我尝试为每个索引分配10

列表a并​​且失败,因为当我尝试将数字分配给10时,数字

是第i个索引的深层副本(这个语句是否正确?)。
In the following code snippet, I attempt to assign 10 to every index in
the list a and fail because when I try to assign number to 10, number
is a deep copy of the ith index (is this statement correct?).



编号不涉及复制。


在分配之前,number是对象的引用, ith

列表中的元素也是指。在作业完成后你会反弹

变量''数字''所以它引用了值10.你不会影响列表

那样。 />

No. There is no copying involved.

Before the assignment, number is a reference to the object to which the ith
element of the list also refers. After the assignment you have rebound the
variable ''number'' so it refers to the value 10. You won''t affect the list
that way.


我的问题是,在for循环中返回第i个索引的值而不是值的动机是什么?

的深度副本

本身?
My question is, what was the motivation for returning a deep copy of
the value at the ith index inside a for loop instead of the value
itself?



没有复制。它返回值本身,或至少一个

引用它。

There is no copying going on. It returns the value itself, or at least a
reference to it.


此外,是否有任何方法可以分配给列表一个for循环(使用

作为上面使用的代码),而不使用枚举?
Also, is there any way to assign to a list in a for loop (with
as little code as used above) without using enumerate?



a [:] = [10] * len(a)


或更多通常类似于:


a = [fn(v)for v in a]


表示涉及该值的一些合适的表达式。注:最后一个表格

保持原始列表不变:如果你真的需要在

中改变它,就像在第一个例子中那样分配给[:],但如果你是更改列表中的所有

元素然后您通常需要一个新列表。

a[:] = [10]*len(a)

or more usually something like:

a = [ fn(v) for v in a ]

for some suitable expression involving the value. N.B. This last form
leaves the original list unchanged: if you really need to mutate it in
place assign to a[:] as in the first example, but if you are changing all
elements in the list then you usually want a new list.


我不太确定你的是什么问,但我会试一试。


你不必使用枚举,你可以使用其他方法就像

范围(len (序列)),但你不能设定值的原因是

,因为for循环迭代一个序列意味着你在[1]中执行
,2,3,4,5]:

...

a只是从序列迭代中拉出的值。


就像我和数字是从迭代中拉出来的价值


for i,枚举数字(a):

...


(措辞严厉:/我道歉。)

Danny Colligan写道:
I''m not quite sure what your asking, but I''ll give it a shot.

You do not have to use enumerate, you can use other methods just as
range(len(sequence)), but the reason you cannot asign a value is
because a for loop iterates a sequence meaning when you do

for a in [1, 2, 3, 4, 5]:
...
a is just a value pulled from the sequence iteration.

just like i and number is a value pulled from the iteration in

for i, number in enumerate(a):
...

(That was worded badly :/ I apologise.)
Danny Colligan wrote:

在下面的代码片段中,我尝试为列表a中的每个索引分配10并且失败,因为当我尝试将数字分配给10时,数字

是深的第i个索引的副本(这个陈述是否正确?)。
In the following code snippet, I attempt to assign 10 to every index in
the list a and fail because when I try to assign number to 10, number
is a deep copy of the ith index (is this statement correct?).

> a = [1,2,3,4, 5]
数字在:
>a = [1,2,3,4,5]
for number in a:



... number = 10

...

... number = 10
...


> a
>a



[1,2, 3,4,5]


所以,我不得不求助于使用枚举来分配列表:

[1, 2, 3, 4, 5]

So, I have to resort to using enumerate to assign to the list:


> for i,number in enumerate(a):
>for i, number in enumerate(a):



... a [i] = 10

...

... a[i] = 10
...


> a
>a



[10,10,10,1 0,10]

我的问题是,在for循环中返回第i个索引的值的深层副本的动机是什么,而不是价值

本身?另外,有没有办法分配到for循环中的列表(使用

作为上面使用的代码)而不使用枚举?


谢谢,


Danny

[10, 10, 10, 10, 10]

My question is, what was the motivation for returning a deep copy of
the value at the ith index inside a for loop instead of the value
itself? Also, is there any way to assign to a list in a for loop (with
as little code as used above) without using enumerate?

Thanks,

Danny


这篇关于为什么不能在没有枚举的循环中分配列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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