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

Python(列表,元组)

一.列表

  1. 列表初识java

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

    1.1 列表的索引编程

    ​ 列表与字符串同样也有索引:数组

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

    1.2 列表的切片app

    ​ 列表与字符串同样可进行切片操做编程语言

    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 列表的操做(增,删,改,查,其余操做)code

    注意 : 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) # ['武当','峨眉','少林',['崆峒',['极乐谷'],'华山'],'魔教','丐帮']

二.元组

  1. 元组初识

    ​ 元组俗称不可变的列表,又称为只读列表,元组也是python的基本数据类型之一,用小括号括起来,里面能够听任何数据类型的数据,循环也能够,查询能够,切片也能够,就是不能改.

    ​ 注意: 这里元组的不可变的意思是当子元素是不可变对象时元素不可变,当子元素为可变对象时是能够变的, 这取决于子元素是不是为可变对象.

    ​ 元组中若是只有一个元素,须要添加一个逗号,不然就不是一个元组,当tu=()时,print(type(tu)),打印结果显示tu是元组.

  2. 元组嵌套

    ​ 元组也有,len(),count(),index()等方法,能够本身尝试直至融会贯通.

    # 元组取值和切片
    tup = ("asd","你好","hell world",123,"升仙")
    print(tup[0]) # asd
    print(tup[2]) # hell world
    print(tup[2:]) # ("hell world",123,"升仙") 切片后仍然是元组
    ---------------------------------
    # for循环
    tup = ("asd","你好","hell world",123,"升仙")
    for el in tup:
     print(el)
    ---------------------------------
    # 当元组内的子元素时可变类型时能够进行操做,将武当换成丐帮
    tup = ("asd","你好","hell world",123,["武当","峨眉","少林"])
    tup[-1][0]="丐帮"
    print(tup) # ("asd","你好","hell world",123,["丐帮","峨眉","少林"])
    # 这里元组的不可变的意思是子元素不可变,而字元素内部的子元素是能够变, 这取决于子元素是不是可变对象.
相关文章
相关标签/搜索