zoukankan      html  css  js  c++  java
  • list(列表) python

    1.list(列表):

    list是处理一组有序项目的数据结构

    list(列表)python中使用最频繁的数据类型

    list中什么类型的数据都可以存放(如:类、自己、函数......);

    list(列表)可以完成大多数数据集合类的数据结构实现,它支持字符、数字、字符串甚至还可以嵌套列表、元组、字典等

    list(列表)用方括号[]标示,内部元素间用逗号隔开。

    2. []空列表:

    >>> a=[]

    >>> a

    []

    3. 非空列表:

    >>> a=[1,2,3]

    >>>

    4. 访问列表:元素、索引、切片

    可以通过索引访问,与字符串的索引一样,列表索引从0开始

    列表可以进行截取、组合等

    >>> list1=["physics","chemistry",1997,2000]

    >>> list2=[1,2,3,4]

    >>> print "list1[0]:",list1[0]

    list1[0]: physics

    >>> print "list2[1:5]:",list2[1:5]

    list2[1:5]: [2, 3, 4]

    >>>

    4.1. 切片:允许返回空

    如:a[len(a):8]

    5. list.insert(index,obj)/list.append(obj):列表中插入/添加对象

    list.insert(index,obj):index的前一个位置插入obj

    list.append(obj):list的最后添加obj

    >>> a=[0,1,2,3]

    >>> type(a)

    <type 'list'>

    >>> isinstance(a,list)

    True

    >>>

    >>> a[1]

    1

    >>> a[-3]

    1

    >>> a[-1]

    3

    >>> a[1:-1]

    [1, 2]

    >>>

    >>> a

    [1, 1, 2, 2, 3, 3]

    >>> a.insert(10,5)

    >>> a

    [1, 1, 2, 2, 3, 3, 5]

    >>>

    >>> a

    [1, 2, 3]

    >>> a.insert(2,5)

    >>> a

    [1, 2, 5, 3]

    >>>

    5.1. 练习:统计列表中的列表元素的个数

    >>> a=[1,[2],[3],"a"]

    >>> c=0

    >>> for i in a:

    ...     if isinstance(i,list):

    ...         c+=1

    ...

    >>> print c

    2

    >>> len(filter(lambda x:isinstance(x,list),a))

    2

    6. list[index]=obj/append/insert:修改/更新列表

    >>> a=[0,"x",1,2,4,6]

    >>> a.append(7)

    >>> a

    [0, 'x', 1, 2, 4, 6, 7]

    >>> a.insert(2,"y")

    >>> a

    [0, 'x', 'y', 1, 2, 4, 6, 7]

    >>> a[1]="m"

    >>> a

    [0, 'm', 'y', 1, 2, 4, 6, 7]

    >>>

    7. del list.[index]/list.remove(obj):删除列表元素 

    删除元素:根据索引、元素

    删除整个列表

    >>> a

    [0, 'm', 'y', 1, 2, 4, 6, 7]

    >>> del a[2]

    >>> a.remove(4)

    >>> a

    [0, 'm', 1, 2, 6, 7]

    >>> a.remove(4)

    Traceback (most recent call last):

      File "<stdin>", line 1, in <module>

    ValueError: list.remove(x): x not in list

    >>>

    7.1. 删除列表元素:

    >>> a=[1,2,4,4,4,4,5,6]

    >>> for i in a:

    ...     if i == 4:

    ...         a.remove(i)

    ...

    >>> a

    [1, 2, 4, 4, 5, 6]

    7.2. 删除列表a中所有的4:

    >>> a=[1,2,3,4,4,4,4,4,6]

    >>> a.count(4)

    5

    >>> for i in range(a.count(4)):

    ...     a.remove(4)

    ...

    >>> a

    [1, 2, 3, 6]

    >>>

    >>> filter(lambda x:x!=4,[1,2,3,4,4,4,4,4,6])

    [1, 2, 3, 6]

    >>>

    >>> "".join(map(str,a))

    '1236'

    >>>

    8. + * 列表运算符:

    +用于组合列表

    *用于重复列表

    >>> [1,2]+[5,6]

    [1, 2, 5, 6]

    >>> ["gloryroad"]*4

    ['gloryroad', 'gloryroad', 'gloryroad', 'gloryroad'] 

    9. 序列(列表)函数&方法:(以下函数对所有序列都可用,序列:字符串、列表、元组)

    9.1. cmp(list1,list2)比较函数

    >>> cmp([1],[2])

    -1

    >>> cmp([3],[2])

    1

    >>> cmp([2],[2])

    0

    >>> cmp([],2)

    1

    >>> cmp([10],[2,2])

    1

    >>> 

    9.2. len(list)列表长度函数

    9.3. max(list)最大值函数:

    >>> max([3],[3,2],[3,3])

    [3, 3]

    >>> max([1,2,3])

    9.4. min(list)最小值函数:

    >>> min([1,2,3])

    9.5. list(seq)转换成列表函数

    >>> list("abc")

    ['a', 'b', 'c']

    >>> list((1,2,3))

    [1, 2, 3]

    >>> list({1:"a",2:"b",3:"c"})

    [1, 2, 3]

    >>>

    10. 列表操作函数:(仅对列表有用)

    10.1. list.append(obj)

    10.2. list.insert(index,obj)

    10.3. list.pop(index)移除列表中的一个元素(默认最后一个元素)并且返回该元素的值

    >>> a=[1,2,3]

    >>> a.pop()

    3

    >>> a.pop()

    2

    >>>

    >>> {1:2,3:4,5:6}.pop(1)

    2

    >>> {1:2,3:4,5:6}.pop(3)

    4

    >>>

    10.4. list.remove(obj)移除列表中某个值的第一个匹配项,一次删除一个

    10.5. list.index(obj)从列表中找出某个值第一个匹配项的索引位置,如果未找到就会抛出异常

    >>> "I am a boy".split().index("boy")

    3

    >>>strObject.index(“element”):返回第几个单词

    10.6. list.count(obj)统计某个元素在列表中出现的次数

    10.7. list.extend(seq)在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)

    >>> a=[1,2,3]

    >>> a.extend([4,5,6])  #每个元素当成一个元素

    >>> a

    [1, 2, 3, 4, 5, 6]

    >>> a.append([4,5,6])  #整体当成一个元素

    >>> a

    [1, 2, 3, 4, 5, 6, [4, 5, 6]]

    >>> 

    10.8. list.reverse()反向列表中元素

    >>> a.reverse()

    >>> a

    [10, 6, 3, 3, 2, -1]

    >>> a=["a","b",1,3,"c",4]

    >>> a.reverse()

    >>> a

    [4, 'c', 3, 1, 'b', 'a']

    >>>

    10.9. list.sort([func])对原列表进行排序,根据ASCII码从大到小进行排序(没有返回值)

    >>> a=[3,2,6,3,10,-1]

    >>> a.sort()

    >>> a

    [-1, 2, 3, 3, 6, 10]

    >>> print a.sort()  #没有返回值

    None 

    10.11. L.sort(cmp=None,key=None,reverse=False)自定义排序:

    L.sort(cmp=None,key=None,reverse=False)

    对原序列进行排序,直接在原序列上操作,没有返回值;

    cmp:为一个定制的比较函数,接受两个参数,并且如果第一个参数小于第二个参数,则返回一个负数(-1),大于则返回一个正数(1),等于则返回0。默认值为None

    key:也是一个函数,这个函数会从每个元素中提取一个用于比较的关键字。默认值为None

    reverse:接受False或者True,表示是否降序。如果设置为True,表示降序。

    例1:

    #每个元素中数字之和排序

    #coding=utf-8

    sentence = "11 22 33 44 394"

    word_list=sentence.split()

    def L(tup) :

        return sum(map(int,list(tup)))  #或reduce(lambda x,y:int(x)+int(y),tup)

    word_list.sort(key = L,reverse = True)

    print word_list

    例2:

    #重写sort中的cmp函数

    #coding=utf-8

    list1 = [(-1,5,3),(-5,3,6,3),(1,1,2,4,5,6),(2,9),(-2,10)]

    def compare(a,b):

        if abs(a)>abs(b):

            return 1

        elif abs(a)==abs(b):

            return 0

        else:

            return -1

    def L(tup) :

        return tup[0]

    list1.sort(cmp=compare,key = L,reverse = False)

    print list1

  • 相关阅读:
    第1年4月22日 IBInspectable巧妙用法 cornerRadius
    第1年4月15日
    第1年4月9日 Domain: com.apple.dt.MobileDeviceErrorDomain
    第1年4月7日 活体检测
    GPS 波段信号范围
    tomcat远程调试
    JdbcTemplate或hibernate动态建表
    jdk动态代理失效,事务自调用失效
    Tomcat 访问静态资源出现中文乱码解决办法(转)
    SQL Server 查看死锁进程(转)
  • 原文地址:https://www.cnblogs.com/reyinever/p/8053524.html
Copyright © 2011-2022 走看看