Python的不堪一击到初学乍练(列表,元组)

2020-12-01 13:36

阅读:709

  1. 列表初识

    • 列表是python的基础数据类型之一,其他编程语言也有类似的数据类型.
      比如JS中的数组, java中的数组等等,它是以[ ]括起来, 每个元素用","隔开并且可以存放各种数据类型.
    • 列表相比于字符串,不仅可以存放不同的数据类型,并且可以存放大量的数据.
      当32位Python可以存放:536870912个元素, 当64位Python可以存放:1152921504606846975个元素,并且列表是按照你保存的顺序进行排序的,有索引并且可以切片,方便取值.
  2. 列表索引,切片和操作

    1.1 列表的索引

    ? 列表与字符串一样也有索引:

    # 列表索引的应用
    lst = ["asd", "你好", "hell world", 123, "升仙"]
    print(lst[0]) # 获取第一个元素
    print(lst[1]) # 获取第二个元素
    print(lst[2]) # 获取第三个元素
    print(lst[3]) # 获取第四个元素
    print(lst[4]) # 获取第五个元素
    # 列表是可变的与字符串不同这点要注意,在列表的操作中会有说明.

    1.2 列表的切片

    ? 列表与字符串一样可进行切片操作

    lst = ["asd","你好","hell world",123,"升仙"]
    print(lst[0:3]) # ["asd","你好","hell world"]
    print(lst[:3]) # ["asd","你好","hell world"]
    print(lst[1::2]) # ["你好",123] 列表同样有步长
    print(lst[2::-1]) # ["hell world","你好","asd"] 步长位置的正负控制着取值的方向
    print(lst[-1:-4:-2]) # ["升仙","hell world"]
    # 列表和字符串的操作基本类似,因此操作起来并不难举一反三即可.

    1.3 列表的操作(增,删,改,查,其他操作)

    注意 : list和str不一样, lst可以发生改变.,所以可以直接在原来list对象上进行操作.

    ? 1.3.1 列表的增加

    # 在列表后新增
    lst = ["asd","你好","hell world",123,"升仙"]
    print(lst)
    lst.append("wusir")
    print(lst) # ["asd","你好","hell world",123,"升仙","wusir"]
    ---------------------------------
    # 根据索引位置插入
    lst = ["asd","你好","hell world",123,"升仙"]
    lst.insert(1,"成仙") # 在索引为1的位置插入刘德华,原来的元素向后移动一位.
    print(lst) # ["asd","成仙","你好","hell world",123,"升仙"]
    ---------------------------------
    # 迭代添加
    lst = ["asd","你好","hell world",123,"升仙"]
    lst2 = ["升仙成功","升仙失败"]
    lst.extend(lst2)
    print(lst) # ["asd","你好","hell world",123,"升仙","升仙成功","升仙失败"]
    print(lst2) # ["升仙成功","升仙失败"]
    # lst2列表添加到lst列表中,lst2本身不变.

    ? 1.3.2 列表的删除

    ? 列表的删除有四种方式(pop,remove,clear,del)

    # pop删除用法
    lst = ["asd","你好","hell world",123,"升仙"]
    print(lst)
    el = lst.pop() # 当pop()括号内为空时,默认删除最后一个索引所对应的的数据.
    print(el) # 打印结果:升仙,pop删除列表的某一项时,会有返回值,其返回值就是被pop删除的内容.
    print(lst) # ["asd","你好","hell world",123]
    
    lst = ["asd","你好","hell world",123,"升仙"]
    el2 = lst.pop(2) # 删除索引为2的位置的值
    print(el2) # hell world
    print(lst) # ["asd","你好",123,"升仙"]
    ---------------------------------
    # remove删除用法
    lst = ["asd","你好","hell world",123,"升仙"]
    lst.remove("升仙") # 可以删除指定元素,当删除不存在的元素会报错
    print(lst) # ["asd","你好","hell world",123]
    ---------------------------------
    # clear删除用法
    lst.clear() # 清空list
    print(lst) # []
    ---------------------------------
    # 切片删除
    del lst[1:3] # 强制删除
    print(lst) # ["asd",123,"升仙"]

    ? 1.3.3 列表的修改

    # 列表的修改
    lst = ["asd","你好","hell world",123,"升仙"]
    lst[1] = "飞升" # 把1号元素修改成飞升
    print(lst) # ["asd","飞升","hell world",123,"升仙"]
    lst[:4:2] = ["返璞归真", "融会贯通"] # 切片修改也可以,同时注意当步长不是1时,元素的个数要一一对应额否则会报错
    print(lst) # ["返璞归真","飞升","融会贯通",123,"升仙"]
    lst[1:4] = ["霸气外露"] # 如果切片没有步长或者步长是1时,则不用管替换的个数.
    print(lst) # ["返璞归真","霸气外露","升仙"]

    ? 1.3.4 列表的查询

    ? 列表是一个可以迭代的对象

    # 用for查询打印列表每一个元素
    lst = ["asd","你好","hell world",123,"升仙"]
    for el in lst:
     print(el)
    ---------------------------------
    # 查询元素在列表中出现的次数
    lst = ["asd","你好","hell world",123,"升仙"]
    c = lst.count("升仙") # 查询升仙出现的次数
    print(c) # 1
    ---------------------------------
    # 查询列表的长度
    lst = ["asd","你好","hell world",123,"升仙"]
    l=len(lst) # 列表的长度,与字符串的使用方式相同
    print(l) # 5

    ? 1.3.4 列表的其他操作

    # 升序
    lst = [1,2,15,23,4,33]
    lst.sort() # 排序,默认为升序.
    print(lst) # [1,2,4,15,23,33]
    ---------------------------------
    # 降序
    lst.sort(reverse=True) # 降序
    print(lst) # [33,23,15,4,2,1]
    ---------------------------------
    # 翻转(倒序)
    lst = ["asd","你好","hell world",123,"升仙"]
    lst.reverse() # 此方法只针对列表内的各个元素,倒序
    print(lst) # ["升仙",123,"hell world","你好","asd"]
  3. 列表嵌套

    一层一层看灵活运用即可,融会贯通.

    lst = ["武当","峨眉","少林",["崆峒",["极乐谷"],"华山"],"魔教"]
    # 找到峨眉
    print(lst[2])
    # 找到少林和魔教
    print(lst[2:])
    # 找到崆峒
    print(lst[3][0])
    # 将崆峒改成丐帮
    lst[3][0]="丐帮"
    print(lst) # ['武当','峨眉','少林',['丐帮',['极乐谷'],'华山'],'魔教']
    # 在lst列表后添加"丐帮"
    lst.append("丐帮")
    print(lst) # ['武当','峨眉','少林',['崆峒',['极乐谷'],'华山'],'魔教','丐帮']


评论


亲,登录后才可以留言!