The most common way to traverse the elements of a list is with a for loop. The syntax is the same as for strings:

This works well if you only need to read the elements of the list. But if you want to write or update the elements, you need the indices. A common way to do that is to combine the functions range and len:

This loop traverses the list and updates each element. len returns the number of elements in the list. range returns a list of indices from 0 to n-1, where n is the length of the list. each time through the loop I gets the index of the next element. The assignment statement in the body uses I to read the old value of the element and to assign the new value.

A for loop over an empty list never executes the body:

Although a list can contain another list, the nested list still counts a single element. The length of this list is four:

from Thinking in Python

Traversing a list的更多相关文章

  1. jQuery 源码分析和使用心得 - 文档遍历 ( traversing.js )

    jQuery之所以这么好用, 首先一点就是$()方法和它强大的选择器. 其中选择器使用的是sizzle引擎, sizzle是jQuery的子项目, 提供高效的选择器查询. 有个好消息告诉大家, 就是s ...

  2. 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  3. HashMap的resize方法中尾部遍历出现死循环问题 Tail Traversing (多线程)

    一.背景介绍: 在看HashMap源码是看到了resize()的源代码,当时发现在将old链表中引用数据复制到新的链表中时,发现复制过程中时,源码是进行了反序,此时是允许反序存储的,同时这样设计的效率 ...

  4. Efficiently traversing InnoDB B+Trees with the page directory--slot

    Efficientlytraversing InnoDB B+Trees with the page directory 1.the purpose of the page directory As ...

  5. HashMap的尾部遍历问题--Tail Traversing

    在看网上HashMap的resize()设计时,提到尾部遍历.   JDK1.7的HashMap在实现resize()时,新table[]的列表采用LIFO方式,即队头插入.这样做的目的是:避免尾部遍 ...

  6. HashMap与TreeMap源码分析

    1. 引言     在红黑树--算法导论(15)中学习了红黑树的原理.本来打算自己来试着实现一下,然而在看了JDK(1.8.0)TreeMap的源码后恍然发现原来它就是利用红黑树实现的(很惭愧学了Ja ...

  7. HashMap的工作原理

    HashMap的工作原理   HashMap的工作原理是近年来常见的Java面试题.几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道HashTable和HashMap之间 ...

  8. Win下必备利器之Cmder

    诚言,对于开发码字者,Mac和Linux果断要比Windows更贴心;但只要折腾下,Windows下也是有不少利器的.之前就有在Windows下效率必备软件一文中对此做了下记载:其虽没oh-my-zs ...

  9. Java多线程系列--“JUC锁”03之 公平锁(一)

    概要 本章对“公平锁”的获取锁机制进行介绍(本文的公平锁指的是互斥锁的公平锁),内容包括:基本概念ReentrantLock数据结构参考代码获取公平锁(基于JDK1.7.0_40)一. tryAcqu ...

随机推荐

  1. iOS七种手势

    // 初始化一个UIimageView UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, ...

  2. zzuoj--10401--物资调度(dfs)

    A.物资调度 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 93  Solved: 52 [Submit][Status][Web Board] De ...

  3. Redis-2-对于key的通用操作

    Redis-2-对于key的通用操作 标签(空格分隔): redis del key key1 key2 作用: 删除1个或多个键 返回值: 不存在的key忽略掉,返回真正删除的key的数量 rena ...

  4. xBIM 基础11 WeXplorer 常用事件

    系列目录    [已更新最新开发文章,点击查看详细]  本篇将介绍查看器在不同场合触发的事件.所有这些都记录在xViewer中. 如果您从Web服务器运行本教程,可以在此处查看完整的实例.并且确保您的 ...

  5. awesome python 中文版 相见恨晚!(pythonNB的第三方资源库)

    Awesome Python中文版来啦! 原文链接:Python 资源大全 内容包括:Web框架.网络爬虫.网络内容提取.模板引擎.数据库.数据可视化.图片处理.文本处理.自然语言处理.机器学习.日志 ...

  6. Android 设置AlertDialog的大小 .

    AlertDialog dialog = builder.setTitle("消息列表") .setView(layout) .create(); dialog.show(); / ...

  7. 004.ES2015和ES2016新特性--块级作用域变量

    其基本原理就是JavaScript的作用域链,下面以对比的方式来展示一下函数级作用域和块级作用域. 函数级作用域 var fns = []; for (var i = 0; i < 5 ; i+ ...

  8. opengl问题

    1.glCreateShader(GL_VERTEX_SHADER)失败原因: 在调用glewInit()初始化之后才能create shader;

  9. 在yii2.0中封装一个生成验证码的控制器

    frontend目录下/封装的验证码类: <?php namespace frontend\controllers; use yii\base\Controller; class CapathC ...

  10. Python链表

    class Node: ''' 节点类 链表节点结构 data next data: 节点保存的数据 _next: 保存下一个节点对象 ''' def __init__(self, data, pne ...