美文网首页
python学习笔记----列表

python学习笔记----列表

作者: 物极必反否极泰来 | 来源:发表于2018-06-15 00:49 被阅读0次

    一、添加:(position 索引.. date数据)
    append(data)
    把添加的值直接当做一个元素插入列表当中(末尾).

    In [9]: list = ['a','b','c','d',]
    In [10]: list
    Out[10]: ['a', 'b', 'c', 'd']
    In [11]: list.append('e')
    In [12]: list
    Out[12]: ['a', 'b', 'c', 'd', 'e']
    #**********************
    In [34]: list
    Out[34]: ['a', 'b', 'c', 'd', 'e']
    In [35]: list1
    Out[35]: ['a', 'b']
    In [36]: list.append(list1)
    In [37]: list
    Out[37]: ['a', 'b', 'c', 'd', 'e', ['a', 'b']]
    extend(data)
    

    会把添加的值拆开成元素,分别插入到列表当中

    In [26]: list
    Out[26]: ['a', 'b', 'c', 'd', 'e']
    In [27]: list1
    Out[27]: ['a', 'b']
    In [28]: list.extend(list1)
    In [29]: list
    Out[29]: ['a', 'b', 'c', 'd', 'e', 'a', 'b']
    

    insert(position,data)

    Out[39]: ['a', 'b', 'c', 'd', 'e']
    In [40]: list.insert(0,'laowang')
    In [41]: list
    Out[41]: ['laowang', 'a', 'b', 'c', 'd', 'e']
    

    二、删除:

    del list[position].

    Out[41]: ['laowang', 'a', 'b', 'c', 'd', 'e']
    In [46]: del list[0]
    In [47]: list
    Out[47]: ['a', 'b', 'c', 'd', 'e']
    

    list.pop() 默认删除末尾

    Out[47]: ['a', 'b', 'c', 'd', 'e']
    In [48]: list
    Out[48]: ['a', 'b', 'c', 'd', 'e']
    In [49]: list.pop()
    Out[49]: 'e'
    In [50]: list
    out[50]: ['a', 'b', 'c', 'd']
    

    list.pop(position) 删除指定索引的值

    Out[52]: ['a', 'b', 'c', 'd']
    In [53]: list.pop(1)
    Out[53]: 'b'
    In [54]: list
    Out[54]: ['a', 'c', 'd']
    

    list.remove(data) 删除指定的值

    Out[54]: ['a', 'c', 'd']
    In [58]: list.remove('a')
    In [59]: list
    Out[59]: ['c', 'd']
    

    三、查:
    x = list[position]

    Out[62]: ['b', 'd']
    In [63]: list
    Out[63]: ['b', 'd']
    In [64]: a = list[1]
    In [65]: a
    Out[65]: 'd'
    

    四、改:
    list[position] = xxx

    Out[60]: ['c', 'd']
    In [61]: list[0] = 'b'
    In [62]: list
    Out[62]: ['b', 'd']
    

    升序

    list.sort()

    In [85]: list
    Out[85]: ['1', '3', '4', '5', '7', '9', '2', '4', '6', '8']
    In [86]: list.sort()
    In [87]: list
    Out[87]: ['1', '2', '3', '4', '4', '5', '6', '7', '8', '9']
    

    list.sort(reverse = False)

    Out[91]: ['1', '3', '4', '5', '7', '9', '2', '4', '6', '8']
    In [92]: list.sort(reverse = False)
    In [93]: list
    Out[93]: ['1', '2', '3', '4', '4', '5', '6', '7', '8', '9']
    

    降序

    list.sort(reverse = True)

    Out[93]: ['1', '2', '3', '4', '4', '5', '6', '7', '8', '9']
    In [94]: list.sort(reverse = True)
    In [95]: list
    Out[95]: ['9', '8', '7', '6', '5', '4', '4', '3', '2', '1']
    

    倒序

    list[::-1]

    Out[95]: ['9', '8', '7', '6', '5', '4', '4', '3', '2', '1']
    In [96]: list[::-1]
    Out[96]: ['1', '2', '3', '4', '4', '5', '6', '7', '8', '9']
    

    list.reverse()

    Out[98]: ['1', '2', '3', '4', '4', '5', '6', '7', '8', '9']
    In [99]: list.reverse()
    In [100]: list
    Out[100]: ['9', '8', '7', '6', '5', '4', '4', '3', '2', '1']
    

    取长度

    len(list)

    Out[100]: ['9', '8', '7', '6', '5', '4', '4', '3', '2', '1']
    In [101]: len(list)
    Out[101]: 10
    

    统计元素个数

    list.count()

    Out[107]: ['9', '8', '7', '6', '5', '4', '4', '3', '2', '1']
    In [108]: list.count('4')
    Out[108]: 2
    

    相关文章

      网友评论

          本文标题:python学习笔记----列表

          本文链接:https://www.haomeiwen.com/subject/httpeftx.html