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

python – 为什么for循环中的值没有改变?

发布时间:2020-12-20 12:08:32 所属栏目:Python 来源:网络整理
导读:为什么范围(len(整数)/ 2)的值在整体修改后不会改变?你在for循环中称为range(len …)值是什么? whole = 'selenium'for i in range(len(whole)/2): print whole whole = whole[1:-1] 输出: seleniumeleniulenien 解决方法 range()生成一次整数列表.该列表
为什么范围(len(整数)/ 2)的值在整体修改后不会改变?你在for循环中称为range(len …)值是什么?

whole = 'selenium'
for i in range(len(whole)/2):
    print whole
    whole = whole[1:-1]

输出:

selenium
eleniu
leni
en

解决方法

range()生成一次整数列表.该列表然后由for循环迭代.每次迭代都不会重新创建;那效率很低.

您可以使用while循环:

i = 0
while i < (len(whole) / 2):
    print whole
    whole = whole[1:-1]
    i += 1

while循环迭代重新测试while条件.

(编辑:李大同)

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

    推荐文章
      热点阅读