zoukankan      html  css  js  c++  java
  • Python学习十三

    访问列表中的值

    使用下标索引来访问列表中的值,同样你也可以使用方括号的形式截取字符,如下所示:

    实例(Python 2.0+)

    #!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5]

    以上实例输出结果:

    list1[0]:  physics
    list2[1:5]:  [2, 3, 4, 5]

    更新列表

    你可以对列表的数据项进行修改或更新,你也可以使用append()方法来添加列表项,如下所示:

    实例(Python 2.0+)

    #!/usr/bin/python # -*- coding: UTF-8 -*- list = [] ## 空列表 list.append('Google') ## 使用 append() 添加元素 list.append('Runoob') print list

    注意:我们会在接下来的章节讨论append()方法的使用

    以上实例输出结果:

    ['Google', 'Runoob']

    删除列表元素

    可以使用 del 语句来删除列表的元素,如下实例:

    实例(Python 2.0+)

    #!/usr/bin/python list1 = ['physics', 'chemistry', 1997, 2000] print list1 del list1[2] print "After deleting value at index 2 : " print list1

    以上实例输出结果:

    ['physics', 'chemistry', 1997, 2000]
    After deleting value at index 2 :
    ['physics', 'chemistry', 2000]

    Python列表截取

    Python 的列表截取实例如下:

    >>>L = ['Google', 'Runoob', 'Taobao'] >>> L[2] 'Taobao' >>> L[-2] 'Runoob' >>> L[1:] ['Runoob', 'Taobao'] >>>

    描述:

    Python 表达式结果描述
    L[2] 'Taobao' 读取列表中第三个元素
    L[-2] 'Runoob' 读取列表中倒数第二个元素
    L[1:] ['Runoob', 'Taobao'] 从第二个元素开始截取列表
  • 相关阅读:
    English,The Da Vinci Code, Chapter 23
    python,meatobject
    English,The Da Vinci Code, Chapter 22
    English,The Da Vinci Code, Chapter 21
    English,The Da Vinci Code, Chapter 20
    English,The Da Vinci Code, Chapter 19
    python,xml,ELement Tree
    English,The Da Vinci Code, Chapter 18
    English,The Da Vinci Code, Chapter 17
    English,The Da Vinci Code, Chapter 16
  • 原文地址:https://www.cnblogs.com/chenyuchun/p/12319354.html
Copyright © 2011-2022 走看看