zoukankan      html  css  js  c++  java
  • Python3

    序列,一种包含多项数据的数据结构,按照顺序排列,通过索引访问其中的数据。Python 常见的序列类型有字符串、列表和元组。

    列表,保存对象的容器,可以存储任意数据类型的数据。所有元素都放在 [ ] 中,用逗号分割每个元素。

    列表是可变的。如果一个容器是可变的,则可以修改容器中的对象。将列表中某个元素的索引赋给一个新的对象,即可改变该元素。

    如果可以使用循环访问对象中的每一个元素,那么该对象是可迭代的,被称为可迭代对象,因此字符串、列表和元组都是可迭代的。可迭代对象中的每一个元素都有一个索引,即表示元素在可迭代对象中位置的数字。列表的正序索引从 0 开始,倒序索引从 -1 开始。

    创建空列表

    >>> list0 = []
    >>> list0, type(list0)
    ([], <class 'list'>)
    

    元素的查询
    1.通过元素的索引来进行列表查询,切片([开始索引:结束索引:步长])列表时遵循顾首不顾尾的原则。

    >>> list0 = ['A', 'B', 'C', 'D', 1, 2, 3, 4]
    >>> list0[0]  # 查单个元素 
    'A'
    >>> list0[1], list0[2]  # 查多个元素
    ('B', 'C')
    >>> list0[0:4]  # 切片
    ['A', 'B', 'C', 'D']
    >>> list0[::2]  # 步长
    ['A', 'C', 1, 3]
    >>> list0[:]  # 开始索引和结束索引不写则查询全部元素
    ['A', 'B', 'C', 'D', 1, 2, 3, 4]
    >>> list0[0:0]  # 开始索引和结束索引为 0 ,返回空列表。
    []
    

    2.index() 方法
    通过元素查索引,有被查询元素则返回元素的索引值,没有则报错:ValueError。

    >>> list0 = ['A', 'B', 'C', 'D', 1, 2, 3, 4]
    >>> list0.index('A')
    0
    >>> list0[list0.index('D')]  # 查找结果返回元素的值:先找索引,在通过索引找元素的值。
    'D'
    >>> list0.index('E')
    Traceback (most recent call last):
      File "<pyshell#16>", line 1, in <module>
        list0.index('E')
    ValueError: 'E' is not in list
    

    元素的修改
    通过索引来修改元素的值。
    注意:索引切片修改时是先把切片匹配到的元素给删除,在插入新的元素,新的元素不限制个数但必须是可迭代的。

    >>> list0 = ['A', 'B', 'C', 'D', 1, 2, 3, 4]
    >>> list0[0] = 'a'
    >>> list0
    ['a', 'B', 'C', 'D', 1, 2, 3, 4]
    >>> list0[1:4] = 'bcef'  # 新元素不限制个数
    >>> list0
    ['a', 'b', 'c', 'e', 'f', 1, 2, 3, 4]
    

    更新时间:2019-08-18

  • 相关阅读:
    How to Create a site at the specified URL and new database (CommandLine Operation)
    Using Wppackager to Package and Deploy Web Parts for Microsoft SharePoint Products and Technologies
    SQL Server Monitor v0.5 [Free tool]
    How to build Web Part
    Deploy web part in a virtual server by developing a Web Part Package file(.cab)
    How to recreate "sites" link if you delete it accidentally
    SharePoint Portal Server管理匿名访问设置
    Monitor sql connection from .Net SqlClient Data Provider
    Brief installation instruction of Sharepoint Portal Server
    How to Use SharePoint Alternate URL Access
  • 原文地址:https://www.cnblogs.com/lipandeng/p/11355289.html
Copyright © 2011-2022 走看看