• 修炼Python基础篇-列表(list)学习


    列表是Python中最常用的数据类型,以一个方括号出现,使用逗号做为分隔值。列表中的每个元素都分配一个数字,它的位置或索引都是从0开始,列表可以进行索引、添加、切片、加、乘或检查成员等。

    1. 访问列表中的元素;

      1. a =[11,22,33,21]
      2. print(a[0])
      3. 11
    2. 更新列表中的元素;

      1. a =[11,22,33,21]
      2. a[0]=1111
      3. print(a)
      4. [1111,22,33,21]
    3. 删除列表中的元素;

      1. a =[11,22,33,21]
      2. del a[0]
      3. print(a)
      4. [22,33,21]
    4. 列表操作符之长度;

      1. a =[11,22,33,21]
      2. print(len(a))
      3. 4
    5. 列表操作符之组合;

      1. a =[11,22,33,21]
      2. b =[99,33,44,66]
      3. c = a + b
      4. print(c)
      5. [11,22,33,21,99,33,44,66]
    6. 列表操作符之重复;

      1. a =[44]*4
      2. print(a)
      3. [44,44,44,44]
    7. 列表操作符之元素是否存在列表中;

      1. a =[11,22,33,21]
      2. print(3in a)
      3. False
      4. print(33in a)
      5. True
    8. 列表操作符之迭代;

      1. for i in[1,2,3,4,5]:
      2. print(i)
      3. 1
      4. 2
      5. 3
      6. 4
      7. 5
    9. 列表操作之列表截取;

      1. a =[11,22,33,21]
      2. a[0]
      3. 11
      4. a[-1]
      5. 21
      6. a[-3]
      7. 22
      8. a[1]
      9. 22
      10. a[2]
      11. 33
      12. a[1:]
      13. [22,33,21]
    10. len:列出元素的个数;

      1. a =[11,22,33,21]
      2. print(len(a))
      3. 4
    11. max:列出列表中最大的元素;

      1. a =[11,22,33,21]
      2. print(max(a))
      3. 33
    12. min:列出列表中小的元素;

      1. a =[11,22,33,21]
      2. print(min(a))
      3. 11
    13. list:将元祖转换为列表;

      1. a =(11,22,33,44)
      2. b = list(a)
      3. print(b)
      4. [11,22,33,44]
      5. print(type(b))
      6. <class'list'>
    14. append:在列表末尾添加新的元素;

      1. a =[11,22,'cc',33,'aa',44,'dd']
      2. a.append(55)
      3. print(a)
      4. [11,22,'cc',33,'aa',44,'dd',55]
    15. extend:在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表);

      1. a =[11,22,'cc',33,'aa',44,'dd']
      2. b =[66,'bb']
      3. a.extend(b)
      4. print(a)
      5. [11,22,'cc',33,'aa',44,'dd',66,'bb']
    16. clear:清空列表中所有的元素;

      1. a =[11,22,'cc',33,'aa',44,'dd']
      2. a.clear()
      3. print(a)
      4. []
    17. copy:复制列表,浅拷贝;

      1. a =[11,22,'cc',33,'aa',44,'dd']
      2. b = a.copy()
      3. print(b)
      4. [11,22,'cc',33,'aa',44,'dd']
    18. count:统计某个元素在列表中出现的次数;

      1. a =[11,22,'cc',33,'aa',44,'dd',44,33,44,'dd']
      2. a.count(44)
      3. 3
    19. index:在列表中找出某个元素第一个匹配项所在的位置;

      1. a =[11,22,'cc',33,'aa',44,'dd',44,33,44,'dd']
      2. a.index(33)
      3. 3
    20. insert:在列表指定某个位置中插入元素;

      1. a =[11,22,'cc',33,'aa',44,'dd',44,33,44,'dd']
      2. a.insert(3,'qq')
      3. a
      4. [11,22,'cc','qq',33,'aa',44,'dd',44,33,44,'dd']
    21. pop:在列表中指定删除一个元素,不指定默认删除末尾元素;

      1. a =[11,22,'cc',33,'aa',44,'dd',44,33,44,'dd']
      2. a.pop()
      3. 'dd'
      4. print(a)
      5. [11,22,'cc',33,'aa',44,'dd',44,33,44]
    22. remove:移出列表中某个值第一个匹配的项,如果没有删除,就返回一个错误值;

      1. a =[11,22,'cc',33,'aa',44,'dd',44,33,44,'dd']
      2. a.remove('cc')
      3. print(a)
      4. [11,22,33,'aa',44,'dd',44,33,44,'dd']
    23. reverse:反向列表中的元素;

      1. a =[11,22,'cc',33,'aa',44,'dd',44,33,44,'dd']
      2. a.reverse()
      3. print(a)
      4. ['dd',44,33,44,'dd',44,'aa',33,'cc',22,11]
    24. sort:排序列表中的元素;

      1. a =[11,22,33,21]
      2. a.sort()
      3. print(a)
      4. [11,21,22,33]
      5. b =['ab','cc','zz','dd']
      6. b.sort()
      7. print(b)
      8. ['ab','cc','dd','zz']
  • 相关阅读:
    Android音视频学习第7章:使用OpenSL ES进行音频解码
    使用cordova+Ionic+AngularJs进行Hybird App开发的环境搭建手冊
    简单脱壳教程笔记(8)---手脱EZIP壳
    linux CentOS安装telnet
    【H.264/AVC视频编解码技术具体解释】十三、熵编码算法(4):H.264使用CAVLC解析宏块的残差数据
    C# 爬取网页上的数据
    ML(1): 入门理论
    ES(6): access elasticsearch via curl
    ES(5): ES Cluster modules settings
    ES(4): ES Cluster Security Settings
  • 原文地址:https://www.cnblogs.com/Saviorsyang/p/6288973.html
Copyright © 2020-2023  润新知