zoukankan      html  css  js  c++  java
  • Traversing a list

    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

  • 相关阅读:
    474 Ones and Zeroes 一和零
    473 Matchsticks to Square 火柴拼正方形
    472 Concatenated Words 连接的单词
    Django 视图系统
    Django 路由系统
    Django 框架基础
    jQuery
    JavaScript- BOM, DOM
    CSS概念,引入,选择器
    HTML
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/3841087.html
Copyright © 2011-2022 走看看