加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 在迭代迭代器时重新分配迭代器的值

发布时间:2020-12-20 11:07:55 所属栏目:Python 来源:网络整理
导读:First of all a disclaimer : I don’t want to use a code like this,I am aware it is a bad practice. As well I am not interested in tips on how to improve it,to make it right. What interests me is a theory. 为什么这样的代码在python 3.6中有效

First of all a disclaimer:

I don’t want to use a code like this,I am aware it is a bad practice. As well I am not interested in tips on how to improve it,to make it right. What interests me is a theory.

为什么这样的代码在python 3.6中有效:

ls = range(5)
for elem_a in ls:

    ls = range(5,10)
    for elem_b in ls:
        print(elem_a,elem_b)

我在迭代它时重新分配ls的值.在ls中第一次执行elem_a时,第一次迭代中的ls值是否存储在内存中?

解决方法

重新分配您正在循环的变量没有任何效果,因为不会为每次迭代重新评估变量.实际上,循环内部循环遍历 iterator,而不是覆盖范围对象.

基本上如果你有这样的循环:

seq = range(5)
for elem in seq:
    seq = something_else

Python将其重写为以下内容:

seq = range(5)

loop_iter = iter(seq)  # obtain an iterator
while True:
    try:
        elem = next(loop_iter)  # get the next element from the iterator
    except StopIteration:
        break  # the iterator is exhausted,end the loop

    # execute the loop body
    seq = something_else

这个的关键方面是循环有自己对iter(seq)的引用存储在loop_iter中,所以自然地重新分配seq对循环没有影响.

所有这些都在compound statement documentation中解释:

06002

The expression list is evaluated once; it should yield an iterable
object. An iterator is created for the result of the expression_list. The suite is then executed once for each item provided by the iterator,in the order returned by the iterator.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读