zoukankan      html  css  js  c++  java
  • 列表类型

    序列是python中最基本的数据结构.序列中的每个元素都分配一个数字(它的位置或索引),第一个索引是0,第二个索引是1,一次类推.

    python有6个序列的内置类型,最常见的是列表和元祖.

    列表是最常用的python数据类型,它可以作为一个方括号内的逗号分隔值出现.

    列表的数据项不需要具有相同的类型.

    创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可.

    list1=['Google','kpan',18,2018]
    list2=[1,2,3,4,5]
    l3=['a','b','c','d']

    总结:存多个值  /  有序  /可变(值变,id不变,可变==不可hash)


    常用操作+内置方法

    1.按索引存取值(正向存取+反向存取):即可存也可以取

    l=['a','b','c','d','e']
    print(l[0])         #取值索引为0的数
    print(l[-1])        #反向取值
    print(id(l))
    l[0]='A'            #将列表中索引为0的存为'A'
    print(l)
    print(id(l))
    
    #输出结果
    a
    e
    2771924504072
    ['A', 'b', 'c', 'd', 'e']
    2771924504072

    2.切片(顾头不顾尾)

    l=[1,2,3,4,5,6]
    
    #正向步长
    l[0:3:1] #[1, 2, 3]
    #反向步长
    l[2::-1] #[3, 2, 1]
    #列表翻转
    l[::-1] #[6, 5, 4, 3, 2, 1]
    l=['a','b','c','d','e']
    print(l[1:4])
    
    #输出结果
    ['b', 'c', 'd']

    3.长度(len)

    l=['a','b','c','d','e']
    print(len(l))
    
    #输出结果
    5

    4.成员运算 in 和 not in

    l=['a','b','c','d','e']
    print('a' in l)
    print('ssssss' not in l)
    
    #输出结果
    True
    True

    5.追加 append() 和插入 insert()    extend

    append()方法用于在列表末尾添加新的对象

    list.append(obj)
    #obj--添加到列表末尾的对象

    返回值: 该方法无返回值,但是会修改原来的列表

    l=['a','b','c','d','e']
    l.append(3333333)
    l.append(44444)
    print(l)
    
    #输出结果
    ['a', 'b', 'c', 'd', 'e', 3333333, 44444]

    insert()函数用于将指定对象插入列表的指定位置

    list.insert(index,obj)
    
    #index--对象obj需要插入的索引位置
    #obj--要插入列表中的对象

    返回值:该方法没有返回值,但会在列表指定位置插入对象.

    l=['a','b','c','d','e']
    l.insert(0,11111111111)
    print(l)
    
    #输出结果
    [11111111111, 'a', 'b', 'c', 'd', 'e']

    extend([1,2,3])  将列表里面的循环取值出来再加入进去

    6.删除( del  pop   remove)

    pop()函数用于移除列表中的一个元素(默认最后一个元素)

    list.pop([index=-1])
    #index-- 可选参数,要移除列表元素的索引值,不能超过列表总长度,默认为index=-1,删除最后一个列表值

    remove()函数用于移除列表中某个值的第一个匹配项.

    list.remove(obj)
    #obj -- 列表中要移除的对象

    返回值:  del()没有返回值

          pop()该方法返回从列表中移除的元素对象.

          remove()没有返回值,但会移除两种中的某个值的第一个匹配项

    l=['a','b','c','d','e']
    del l[0]
    res=l.remove('b')
    print(l)
    print(res)
    
    res=l.pop(0)
    print(l)
    print(res)
    
    #输出结果
    ['c', 'd', 'e']
    None
    ['d', 'e']
    c
    l=[]
    #队列:先进先出
    #入队
    l.append('first')
    l.append('second')
    l.append('third')
    print(l)
    #出队
    print(l.pop(0))
    print(l.pop(0))
    print(l.pop(0))
    
    #堆栈:后进先出
    #入栈
    l.append('first')
    l.append('second')
    l.append('third')
    print(l)
    #出栈
    print(l.pop())
    print(l.pop())
    print(l.pop())
    
    #输出结果
    ['first', 'second', 'third']
    first
    second
    third
    ['first', 'second', 'third']
    third
    second
    first
    队列(先进先出) 堆栈(后进先出)

    7.循环

    l=['a','b','c','d','e']
    for item in l:
        print(item)
    
    #输出结果
    a
    b
    c
    d
    e
  • 相关阅读:
    扯一扯纯函数
    10.28
    10.27 动手动脑5
    10.26
    10.25 周总结
    10.23
    10.22
    10.21 动手动脑4
    10.20
    10.19
  • 原文地址:https://www.cnblogs.com/kp1995/p/10001786.html
Copyright © 2011-2022 走看看