美文网首页
列表、元组、字典三种高级变量

列表、元组、字典三种高级变量

作者: 岑洋 | 来源:发表于2018-11-29 10:49 被阅读0次

    自学整理记录,大神见笑

    列表

    • 列表是Python中使用最频繁的数据类型,类似Java的数组
    • 列表用[]定义,数据之间用,分隔
    • 列表的索引从0开始
    • 索引就是列表中的位置编号,也叫下标

    列表的方法

    1.增

    1.1 在索引处插入数据→insert

    • 列表.insert(索引,数据)

    list_one = [1, 2, 3]
    print list_one
    list_one.insert(1, 9)
    print list_one

    • 在列表list_one索引1处插入数字9

    • 输出如下:


      insert1.jpg
    • 注:如果索引大于原列表长度,则会追加到原列表上,如下

    list_one = [1, 2, 3]
    print list_one
    list_one.insert(10, 9)
    print list_one

    • 输出如下:
    insert2.png

    1.2 在末尾追加数据→append

    • 列表.append(数据)

    list_one = [1, 2, 3]
    print list_one
    list_one.append(9)
    print list_one

    • 将9追加到list_one列表末尾
    • 输出如下:
    append.png

    1.3 将列表2的数据追加到列表1→extend

    • 列表1.extend(列表2)

    list_one = [1, 2, 3]
    list_two = [1, 8, 9]
    print list_one
    list_one.extend(list_two)
    print list_one

    • 将list_two的列表元素追加到list_one中
    • 输出如下:
    extend.png

    2.删

    2.1 删除指定索引的数据→del

    • del 列表[索引]

    list_one = [1, 2, 3]
    print list_one
    del list_one[1]
    print list_one

    • 将列表list_one索引1的位置的元素删除
    • 输出如下:
    del.png
    • 这里要注意的是:del方法里的参数索引值需要在列表长度范围内,否则会报错,如下

    list_one = [1, 2, 3]
    print list_one
    del list_one[4]
    print list_one

    • list_one长度为3,最大索引值2,而函数del的参数索引值为4,会报错
    • 输出如下:
    del_error.png

    2.2 删除第一次出现的指定数据→remove

    • 列表.remove(数据)

    list_one = [1, 2, 3]
    print list_one
    list_one.remove(2)
    print list_one

    • 将列表list_one中的元素2删除
    • 输出如下:
    remove.png
    • 这里要注意的是:remove方法的参数数据必须是列表中存在的,否则会报错,如下

    list_one = [1, 2, 3]
    print list_one
    list_one.remove(6)
    print list_one

    • 输出如下:
    remove_error.png

    2.3 删除指定索引的数据→pop

    • 列表.pop[索引]

    list_one = [1, 2, 3]
    print list_one
    list_one.pop(1)
    print list_one

    • 将列表list_one中索引为1位置的元素删除
    • 输出如下:
    pop1.png
    • 注:如果没有在pop方法中填入参数,则会默认删除列表最后一位元素,如下

    list_one = [1, 2, 3]
    print list_one
    list_one.pop()
    print list_one

    • 删除列表list_one最后一位元素
    • 输出如下
    pop2.png
    • 注:如果pop方法中的参数索引值不能大于列表的长度,否则则会报错,如下

    list_one = [1, 2, 3]
    print list_one
    list_one.pop(4)
    print list_one

    • 列表list_one最大长度为3,最大索引为2,但pop方法中的参数为4,删除索引为4的位置
    • 输出如下
    pop3.png

    2.4 清空列表→clear

    • 列表.clear

    list_one = [1, 2, 3]
    print(list_one)
    list_one.clear()
    print(list_one)

    • 清空列表list_one中的元素
    • 输出如下:
    clear1.jpg
    • 这里要注意的是:clear该方法只能在python3以上使用,python2会报错,输出如下
    clear2.png

    3.改

    • 列表[索引] = 数据

    list_one = [1, 2, 3]
    print(list_one)
    list_one[1] = 7
    print(list_one)

    • 修改列表list_one索引值为1的元素为7
    • 输出如下:
    改.png

    4.查

    4.1 列表[索引]

    • 列表[索引]

    list_one = [1, 2, 3]
    print(list_one)
    a = list_one[1]
    print(a)

    • 查询列表list_one中索引值为1的元素
    • 输出如下:
    查1.png
    • 注:索引值不能大于列表的长度,否则会报错,如下

    list_one = [1, 2, 3]
    print(list_one)
    a = list_one[4]
    print(a)

    • 输出如下:
    查2.png

    4.2 查找该数据第一次出现的索引→index

    • 列表.index(数据, 开始查找的位置, 结束查找的位置)

    list_one = [1, 2, 3, 2, 4, 5, 2, 6]
    print(list_one)
    a = list_one.index(2)
    print(a)

    • 查询列表list_one中第一次出现元素2的位置
    • 输出如下:
    index1.png
    • 注:可以在index方法中填入查询范围,在索引a到索引b之间查询,如下

    list_one = [1, 2, 3, 2, 4, 5, 2, 6]
    print(list_one)
    a = list_one.index(2, 2, 4)
    print(a)

    • 在列表list_one中在索引2到索引4之间(左闭右开)查询元素2的位置
    • 输出如下:
    index2.png
    • 注:如果数据在列表中查询不到,则会报错,如下

    list_one = [1, 2, 3, 2, 4, 5, 2, 6]
    print(list_one)
    a = list_one.index(2, 0, 1)
    print(a)

    • 在列表list_one索引0到1之间(左闭右开)查询数据2的位置
    • 输出如下:
    index3.png

    5.统计

    5.1 列表长度→len

    • len(列表)

    list_one = [1, 2, 3, 2, 4, 5, 2, 6]
    print(list_one)
    a = len(list_one)
    print(a)

    • 查询列表list_one的长度
    • 输出如下:
    len.png

    5.2 数据在列表中出现的次数→count

    • 列表.count(数据)

    list_one = [1, 2, 3, 2, 4, 5, 2, 6]
    print(list_one)
    a = list_one.count(2)
    print(a)

    • 查询列表list_one中元素2出现的次数
    • 输出如下:
    count1.png
    • 注:如果列表中不存在count查询的元素,则返回0,如下

    list_one = [1, 2, 3, 2, 4, 5, 2, 6]
    print(list_one)
    a = list_one.count(7)
    print(a)

    • 查询列表list_one中数据7出现的次数
    • 输出如下:
    count2.png

    6.排序

    6.1 排序→sort

    • 列表.sort()

    list_one = [1, 2, 3, 2, 4, 5, 2, 6]
    print(list_one)
    list_one.sort()
    print(list_one)

    • 将列表list_one正序排序
    • 输出如下:
    sort1.png
    • 注:当然字母也可以排序,同时可以混合排序,排序规则为数字在前,然后是字母大写,然后是字母小写,如下

    list_one = ["c", "d", "b", "a", "t", "z", "r", "d", "C", 6]
    print(list_one)
    list_one.sort()
    print(list_one)

    • 将列表list_one正序排序
    • 输出如下:
    sort2.png
    • 注:如果需要将列表倒序排序,只需要在sort方法中加一参数reverse=True即可,如下

    list_one = ["c", "d", "b", "a", "t", "z", "r", "d", "C", 6]
    print(list_one)
    list_one.sort(reverse=True)
    print(list_one)

    • 将列表list_one倒序排序
    • 输出如下:
    sort3.png

    6.2 反转→reverse

    • 列表.reverse()

    list_one = ["c", "d", "B", 6, "a", "T", "z", "r", "d", "C"]
    print(list_one)
    list_one.reverse()
    print(list_one)

    • 将列表list_one反转
    • 输入如下:
    sort4.png

    关键字、函数和方法

    • 关键字是Python内置,不需要小括号
    • 函数封装独立功能,可直接调用
    • 方法封装独立功能,由对象调用,表示针对这个对象要做的操作

    循环遍历

    • 使用for实现迭代遍历

    # for 循环内部使用的变量 in 列表
    for name in name_list:
     循环内部针对列表元素进行操作
     print(name)

    列表应用场景

    • 列表可以存放不同类型的数据,但实际应用一般都是存储相同类型的数据

    元组

    元组定义

    • 元组的元素不能修改
    • 元组用()定义
    • 元组也用分隔
    • 元组也是从0开始
    • 元组是tuple

    定义元组

    • 空元组

    empty_tuple = ()

    • 只包含一个元素的元组,在这个元素后加,号,否则系统会认为是int类型

    single_tuple = (5,)

    • 定义正常元组

    tuple = ("zhangsan", 18, 1.75)

    元组的方法

    1.查

    1.1 获取索引位置的数据→元组[索引]

    • 元组[索引]

    tuple_one = (1, 2, 3)
    print(tuple_one)
    a = tuple_one[0]
    print(a)

    • 查询元组tuple_one中索引为0的元素
    • 输出如下:
    tuple1.jpg

    1.2 查找该数据第一次出现的索引→index

    • 元组.index(数据, 开始查找的位置, 结束查找的位置)

    tuple_one = (1, 2, 3)
    print(tuple_one)
    a = tuple_one.index(2)
    print(a)

    • 查询元组中tuple_one出现元素2的第一次位置
    • 输出如下:
    tuple2.png

    2.统计

    2.1 获取该数据出现的次数→count

    • 元组.count(数据)

    tuple_one = (1, 2, 3, 2, 3, 2)
    print(tuple_one)
    a = tuple_one.count(2)
    print(a)

    • 查询元组tuple_one中元素2出现的次数

    • 输出如下:


      count1.jpg
    • 注:如果要查询的元素不在元组中,则返回0,如下

    tuple_one = (1, 2, 3, 2, 3, 2)
    print(tuple_one)
    a = tuple_one.count(7)
    print(a)

    • 查询元组tuple_one中元素7出现的次数
    • 输出如下:
    count2.png

    2.2 获取元组的长度→len

    • len(元组)

    tuple_one = (1, 2, 3, 2, 3, 2)
    print(tuple_one)
    a = len(tuple_one)
    print(a)

    • 查询元组tuple_one的长度
    • 输出如下:
    len.png

    循环遍历

    • 方法同列表,使用for循环
    • 元组循环遍历中使用格式化字符串不方便,因为一般情况元组中的元素数据类型不一样

    元组应用场景

    • 用于函数的参数和返回值
    • 让列表不可修改,安全
    • 格式字符串,%后面的()就是一个数组

    列表和元组相互转换

    • 列表转元组

    tuple(列表)

    • 元组转列表

    list(元组)

    字典

    字典定义

    • 字典也可以存储多个数据
    • 字典用{}定义
    • 字典无序,列表有序
    • 字典也使用分隔
    • 字典使用键值对存储数据
      1.键key是索引
      2.值value是数据
      3.键和值之间用分隔
      4.键必须唯一
      5.值可以是任意数据类型,但键只能使用字符串、数字或元组
    • 格式如下:

    xiaoming = {"name": "小明", "age": 18, "gender": True, "height": 1.75}

    • 注意:书写格式最好一行只有一个键值对
    • 注:因为字典无序,所以控制台输出可能和定义的顺序不一致

    字典方法

    1.增

    • 字典[key] = value

    dict_one = {"name": "zhangsan", "age": 18, "sex": True}
    print(dict_one)
    dict_one["phone"] = 666
    print(dict_one)

    • 在字典dict_one中增加一个键值对phone=666
    • 输出如下:
    dict1.jpg

    2.删

    • 字典.pop(key)

    dict_one = {"name": "zhangsan", "age": 18, "sex": True}
    print(dict_one)
    dict_one.pop("sex")
    print(dict_one)

    • 删除字典dict_one中键位sex的键值对
    • 输出如下:
    dict2.png
    • 注:pop方法要删除的键值必须存在,否则报错,如下

    dict_one = {"name": "zhangsan", "age": 18, "sex": True}
    print(dict_one)
    dict_one.pop("phone")
    print(dict_one)

    • 删除字典dict_one中键为phone的键值对
    • 输出如下:
    dict3.png

    3.改

    • 字典[key] = value

    dict_one = {"name": "zhangsan", "age": 18, "sex": True}

    print(dict_one)
    dict_one["sex"] = False
    print(dict_one)

    • 修改字典dict_one中的sex键的值为False
    • 输出如下:
    dict4.jpg

    4.查

    • 字典[key]

    dict_one = {"name": "zhangsan", "age": 18, "sex": True}
    print(dict_one)
    a = dict_one["name"]
    print(a)

    • 查询字典dict_one中name键对应的值
    • 输出如下:
    dict5.png

    5.统计

    • len(字典)

    dict_one = {"name": "zhangsan", "age": 18, "sex": True}
    print(dict_one)
    a = len(dict_one)
    print(a)

    • 查询字典dict_one的长度
    • 输出如下:
    dict6.png

    6.合并

    • 字典1.update(字典2)

    dict_one = {"name": "zhangsan", "age": 18, "sex": True}
    dict_two = {"name": "lisi", "phone": 77, "email": "114.com"}
    print(dict_one)
    print(dict_two)
    dict_one.update(dict_two)
    print(dict_one)
    print(dict_two)

    • 将字典2 dict_two的数据追加到字典1 dict_one中,如果数据的键值重复,则取字典2 dict_two中的值
    • 输出如下:
    dict7.png

    7.清空

    • 字典.clear

    dict_one = {"name": "zhangsan", "age": 18, "sex": True}
    print(dict_one)
    dict_one.clear()
    print(dict_one)

    • 将字典dict_one清空数据
    • 输出如下:
    dict8.png

    字典循环遍历

    • 因字典的value数据类型不确定,所以实际开发,遍历字典需求较少
    • 格式如下:

    # for 新定义循环内部使用的key的变量 in 字典
    for k in xiaoming:
     print("%s: %s" % (k, xiaoming[k]))

    字典和列表应用场景

    • 实际开发更多的应用场景是
      1.使用多个键值对,存储描述一个物体的相关信息
      2.然后将多个这种字典放在一个列表中,再进行遍历,每个循环体内针对每一个字典进行相同的处理

    相关文章

      网友评论

          本文标题:列表、元组、字典三种高级变量

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