zoukankan      html  css  js  c++  java
  • python学习笔记(七)之列表

    列表:是一个加强版的数组,什么东西都可以往里面放。

    创建列表

    创建一个普通列表:

    1 >>> member = ['operating system', 'data structure', 'network', 'principle of computer composition']
    2 >>> member
    3 ['operating system', 'data structure', 'network', 'principle of computer composition']
    View Code

    创建一个混合列表:

    1 >>> mix = [1, 'hello python', 3.14159, [1,2,3,['one','two']]]
    2 >>> mix
    3 [1, 'hello python', 3.14159, [1, 2, 3, ['one', 'two']]]
    View Code

    创建一个空列表:

    1 >>> empty = []
    2 >>> empty
    3 []
    View Code

    向列表中添加元素

    使用append(),extend(),和insert()方法

    append()在尾部添加,extend()用一个列表来扩展列表,insert(index, obj)在index处插值。

    1 >>> member.append('mechine learning')
    2 >>> member
    3 ['operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning']
    4 >>> member.extend(['data mining', 'hadoop'])
    5 >>> member
    6 ['operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning', 'data mining', 'hadoop']
    7 >>> member.insert(0,'PE')
    8 >>> member
    9 ['PE', 'operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning', 'data mining', 'hadoop']
    View Code

     从列表中获取元素

    和数组一样,通过元素的索引从列表中获得单个元素,索引从0开始。

     1 >>> for i in range(len(member)):
     2 ...     print(member[i])
     3 ... 
     4 PE
     5 operating system
     6 data structure
     7 network
     8 principle of computer composition
     9 mechine learning
    10 data mining
    11 hadoop
    View Code

    通过列表分片

    1 >>> member[3]
    2 'network'
    3 >>> member[:3]
    4 ['PE', 'operating system', 'data structure']
    5 >>> member[3:]
    6 ['network', 'principle of computer composition', 'mechine learning', 'data mining', 'hadoop']
    7 >>> member[3:5]
    8 ['network', 'principle of computer composition']
    View Code

    从列表中删除元素

    使用remove(),pop()方法或del

     1 >>> member
     2 ['PE', 'operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning', 'data mining', 'hadoop']
     3 >>> member.remove('hadoop')
     4 >>> member
     5 ['PE', 'operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning', 'data mining']
     6 >>> member.pop()
     7 'data mining'
     8 >>> member
     9 ['PE', 'operating system', 'data structure', 'network', 'principle of computer composition', 'mechine learning']
    10 >>> member.pop(2)
    11 'data structure'
    12 >>> member
    13 ['PE', 'operating system', 'network', 'principle of computer composition', 'mechine learning']
    14 >>> del member[3]
    15 >>> member
    16 ['PE', 'operating system', 'network', 'mechine learning']
    17 >>> del member
    18 >>> member
    19 Traceback (most recent call last):
    20   File "<stdin>", line 1, in <module>
    21 NameError: name 'member' is not defined
    View Code

     列表的一些常用操作符

      比较操作符 >, >=, < ,<=, == ,!=

    1 >>> list1 = [123]
    2 >>> list2 = [456]
    3 >>> list1 < list2
    4 True
    5 >>> list1 = [123,456]
    6 >>> list2 = [234,123]
    7 >>> list1 > list2
    8 False
    View Code

      逻辑操作符 and, or, not

    1 >>> list3 = [123,456]
    2 >>> list1 < list2 and list1 == list3
    3 True
    4 >>> list1 > list2 or list1 < list2
    5 True
    6 >>> not list1 < list2
    7 False
    View Code

      拼接操作符 *

    1 >>> list4 = list2 + list3
    2 >>> list4
    3 [234, 123, 123, 456]
    View Code

      重复操作符 *

    1 >>> list4
    2 [234, 123, 123, 456]
    3 >>> list4 * 5
    4 [234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456, 234, 123, 123, 456]
    View Code

      成员关系操作符in, not in

    1 >>> 123 in list4
    2 True
    3 >>> 345 in list4
    4 False
    5 >>> 345 not in list4
    6 True
    View Code

     列表一些常用的方法

      count():返回列表内某个成员出现次数,如果不存在则返回0

    1 >>> list4
    2 [234, 123, 123, 456]
    3 >>> list4.count(123)
    4 2
    View Code

      index():返回该元素在列表第一次出现位置的索引,若不存在,则抛出一个ValueError异常。

    1 >>> list4.index(123)
    2 1
    3 >>> list4.index(124)
    4 Traceback (most recent call last):
    5   File "<stdin>", line 1, in <module>
    6 ValueError: 124 is not in list
    View Code

      reverse():翻转列表

    1 >>> list4
    2 [456, 123, 123, 234]
    3 >>> list4.reverse()
    4 >>> list4
    5 [234, 123, 123, 456]
    View Code
  • 相关阅读:
    硬盘任性丢数据,但分布式存储一定可靠吗?
    Service的基本组成
    固定cell.imageView.image的大小
    剪贴板服务
    取得正在运行的Activity
    取得正在运行的服务
    C#.NET学习笔记1---C#.NET简介
    取得手机的网络信息
    四、cocos2dx动画Animation介绍
    C#.NET学习笔记2---C#.第一个C#程序
  • 原文地址:https://www.cnblogs.com/ZGreMount/p/7758440.html
Copyright © 2011-2022 走看看