zoukankan      html  css  js  c++  java
  • 基本数据类型及内置方法之列表

    列表(list)

    定义:在[]内,用逗号分隔开多个任意数据类型的值

    l1 = [1, 2, 3, 4] #本质为:l1 = list[1, 2, 3, 4]

    用途:用于存一个或多个不同类型的值

    类型转换

    # 但凡能被for循环遍历的数据类型都可以传给list()转换成列表类型,list()会跟for循环一样遍历出数据类型中包含的每一个元素然后放到列表中
    >>> list('wdad') # 结果:['w', 'd', 'a', 'd']
    >>> list([1,2,3]) # 结果:[1, 2, 3]
    >>> list({"name":"jason","age":18}) #结果:['name', 'age']
    >>> list((1,2,3)) # 结果:[1, 2, 3]
    >>> list({1,2,3,4}) # 结果:[1, 2, 3, 4]

    优先掌握的方法

    索引取值

    按索引存取值(正向存取+反向存取):即可存也可以取
    l1 = ['tony', 'jason', 'tom', 4, 5]
    print(l1[0])  #正取
    tony
    print(l1[-1]) #反取
    5
    l1[1] = 'sean'#按照索引修改指定位置的值,如果索引不存在则报错
    print(l1)
    ['tony', 'sean', 'tom', 4, 5]

    索引切片

    #顾头不顾尾:取出索引为0到3的元素
    l1 = ['tony', 'jason', 'tom', 4, 5]
    print(l1[0:3])
    ['tony', 'jason', 'tom']
    #也可以设置步长
    print(l1[0:4:2])
    ['tony', 'tom']

    成员运算

    in,not in

    #返回结果为bool值
    l1 = ['tony', 'jason', 'tom', 4, 5]
    print('tom'in l1)
    True
    print('bing' not in l1)
    True

    len()

    #获取列表中元素的个数
    l1 = ['tony', 'jason', 'tom', 4, 5]
    print(len(l1))
    5

    添加

    #append()在列表尾部追加元素
    l1 = ['tony', 'jason', 'tom', 4, 5]
    l1.append(1)
    print(l1)
    ['tony', 'jason', 'tom', 4, 5, 1]

    #extend()一次在列表尾部添加多个元素
    l1 = ['tony', 'jason', 'tom', 4, 5]
    l1.extend(['bing', 2, 3])
    print(l1)
    ['tony', 'jason', 'tom', 4, 5, 1, 'bing', 2, 3]

    #insert()在指定位置插入元素,必须指定索引位置
    l1 = ['tony', 'jason', 'tom', 4, 5]
    l1.insert(0, 'sean')
    print(l1)
    ['sean', 'tony', 'jason', 'tom', 4, 5]

    删除

    # del 万能删,根据索引删除
    l1 = ['tony', 'jason', 'tom', 4, 5]
    del(l1[1])
    print(l1)
    ['tony', 'tom', 4, 5]

    # pop()默认弹出列表最后一位元素,还可以指定索引弹出,返回被弹出的元素
    #不指定索引弹出
    l1 = ['tony', 'jason', 'tom', 4, 5]
    print(l1.pop()) #直接输出弹出的元素
    5
    print(l1)
    ['tony', 'jason', 'tom', 4]
    l1.pop() #默认弹出最后一个元素后,再返回列表
    print(l1)
    ['tony', 'jason', 'tom']

    #指定索引弹出
    l1 = ['tony', 'jason', 'tom', 4, 5]
    print(l1.pop(2))
    tom

    排序

    #sort()给列表内所有元素排序
    #排序时列表元素之间必须时相同的数据类型,不可混搭,否则报错
    l1 = [1, 7, 6, 4, 5, 2, 3]
    l1.sort() #默认从小到大
    print(l1)
    [1, 2, 3, 4, 5, 6, 7]
    l1.sort(reverse=True) #可以设置倒序
    print(l1)
    [7, 6, 5, 4, 3, 2, 1]

    #单独使用的意思为颠倒列表内元素顺序
    l1 = [1, 7, 6, 4, 5, 2, 3]
    l1.reverse()
    print(l1)
    [3, 2, 5, 4, 6, 7, 1]

    count()

    #统计当前列表内指定的元素个数
    l1 = [1, 2, 3, 3, 4]
    print(l1.count(3))
    2

    index()

    #获取当前指定元素的索引值,可以指定查找范围
    l1 = [1, 2, 3, 4]
    print(l1.index(1, 0, 2))#在索引0到2之间获取1的索引位置
    0

    clear()

    #清除列表数据
    l1 = [1, 2, 3, 4]
    l1.clear()
    print(l1)

    for循环

    l1 = [1, 2, 3, 4]
    for i in l1:
       print(i)
    1
    2
    3
    4

    总结:列表为有序 可变 多个值的类型

  • 相关阅读:
    js optional chaining operator
    Linux Bash Script conditions
    Linux Bash Script loop
    js Nullish Coalescing Operator
    js sort tricks All In One
    React Portal All In One
    最小生成树&&次小生成树
    链式前向星实现以及它的遍历
    [2015.11.8|9图论]解题代码集合
    最短路算法及其延伸
  • 原文地址:https://www.cnblogs.com/a736659557/p/11807448.html
Copyright © 2011-2022 走看看