• Python开发——3.基本数据类型之列表、元组和字典


    一、列表(list)

    1.列表的格式

      li = [11,22,"kobe",["lakers","ball",11],(11,22,),{"k1":11,"k2":"kuzma"},True,False]

    2.列表可以嵌套任何格式,数字、字符串、列表、元组、字典、布尔值。

    3.索引取值

    li = [11,22,"kobe",["lakers","ball",11],(11,22,),{"k1":11,"k2":"kuzma"},True,False]
    print(li[1])

    4.切片取值

    li = [11,22,"kobe",["lakers","ball",11],(11,22,),{"k1":11,"k2":"kuzma"},True,False]
    print(li[1:-1])

    5.for循环

    li = [11, 22, "kobe", ["lakers", "ball", 11], (11, 22,), {"k1": 11, "k2": "kuzma"}, True, False]
    for item in li:
        print(item)

    6.利用索引修改和删除

      a.修改

    li = [11, 22, "kobe", ["lakers", "ball", 11], (11, 22,), {"k1": 11, "k2": "kuzma"}, True, False]
    li[2] = "randle"
    print(li)

      b.删除

    li = [11, 22, "kobe", ["lakers", "ball", 11], (11, 22,), {"k1": 11, "k2": "kuzma"}, True, False]
    del li[1]
    print(li)

    7.利用切片修改和删除

      a.修改

    li = [11, 22, "kobe", ["lakers", "ball", 11], (11, 22,), {"k1": 11, "k2": "kuzma"}, True, False]
    li[0:2] = [33,44]
    print(li)

      b.删除

    li = [11, 22, "kobe", ["lakers", "ball", 11], (11, 22,), {"k1": 11, "k2": "kuzma"}, True, False]
    del li[0:4]
    print(li)

    8.in 操作

    li = [11, 22, "kobe", ["lakers", "ball", 11], (11, 22,), {"k1": 11, "k2": "kuzma"}, True, False]
    v ="ball" in li
    print(v)

    9.查找

    li = [11, 22, "kobe", ["lakers", "ball", 11], (11, 22,), {"k1": 11, "k2": "kuzma"}, True, False]
    v = li[3][2]
    print(v)

    10.转换

    (1)字符串转列表

    s = "hechouzi"
    li = list(s)
    print(li)

    (2)列表转字符串

      a.列表中有数字,要用for循环

    li = [11, 22, "kobe", ["lakers", "ball", 11], (11, 22,), {"k1": 11, "k2": "kuzma"}, True, False]
    s = ""
    for item in li:
        s = s + str(item)
    print(type(s),s)

      b.列表中只有字符串

    li = ["123","hahah"]
    v = "".join(li)
    print(v)

    11.方法

    (1)append 原来值最后追加

    li = [11, False, "kobe", ["lakers"], (11, 22,), {"k1": 11, "k2": "kuzma"}]
    li.append(22)
    print(li

    (2)clear 清空列表

    li = [11, False, "kobe", ["lakers"], (11, 22,), {"k1": 11, "k2": "kuzma"}]
    li.clear()
    print(li)

    (3)copy (浅拷贝)

    li = [11, False, "kobe", ["lakers"], (11, 22,), {"k1": 11, "k2": "kuzma"}]
    v = li.copy()
    print(v)

    (4)count 计算元素出现的次数

    li = [11, False,0, "kobe", ["lakers"], (11, 22,), {"k1": 11, "k2": "kuzma"}]
    v = li.count(False)
    print(v)

    (5)extend 扩展原列表,参数:可迭代对象

    li = [11, False,0, "kobe", ["lakers"], (11, 22,), {"k1": 11, "k2": "kuzma"}]
    li.extend([(11,22),"33"])
    print(li)

    (6)index 根据值获取当前值的索引位置(左边优先)

    li = [11, False,0, "kobe", ["lakers"], (11, 22,), {"k1": 11, "k2": "kuzma"}]
    v = li.index(False)
    print(v)

    (7)insert 在指定索引位置插入元素

    li = [11, False,0, "kobe", ["lakers"], (11, 22,), {"k1": 11, "k2": "kuzma"}]
    li.insert(2,"randle")
    print(li)

    (8)pop(1:指定索引;2:默认删除最后一个) 删除某个值,并获取删除的值

    li = [11, False,0, "kobe", ["lakers"], (11, 22,), {"k1": 11, "k2": "kuzma"}]
    v = li.pop()
    print(li,v)

    (9)remove 删除列表中指定的值,左边优先

    li = [11,22,33,33,44]
    li.remove(33)
    print(li)

    (10)reverse 将列表进行翻转

    li = [11,22,33,33,44]
    li.reverse()
    print(li)

    (11)sort 排序

    li = [11,22,33,33,44]
    li.sort(reverse = True)
    print(li)

    二、元组(tuple)

    1.元组的写法,一般在最后加,

    tu = (11,"hehcouzi",)

    2.元组的一级元素不可被修改,不能增加或删除

    3.索引取值

    tu = (11,"kobe",[22],{"key1":"ball"},)
    v = tu [1]
    print(v)

    4.切片取值

    tu = (11,"kobe",[22],{"key1":"ball"},)
    v = tu [1:-1]
    print(v)

    5.for 循环

    tu = (11, "kobe", [22], {"key1": "ball"},)
    for item in tu:
        print(item)

    6.转换

    s = "hehcouzi"
    v1 = tuple(s)
    v2 = list(v1)
    print(v1,v2

    三、字典(dict)

    1.字典的基本结构

    info = {“k1”:"v1","k2":"v2"}.key:value

    2.字典的value可以是任何值

    3.字典和列表不可做key值

    4.字典是无序的

    5.可以通过索引取值,不能切片

    info = {"k1":11,"k2":"hechouzi","k3":("lakers"),1:22,True:11,}
    v = info["k1"]
    print(v)

    6.del删除

    info = {"k1":11,"k2":"hechouzi","k3":("lakers"),1:22,True:11,}
    del info["k1"]
    print(info)

    7. for 循环,默认输出key值

      a. info.items

      输出key和value

    info = {"k1":11,"k2":"hechouzi","k3":("lakers"),1:22,True:11,}
    for item in info.items():
        print(item)

      b. info.values

       输出value值

    info = {"k1":11,"k2":"hechouzi","k3":("lakers"),1:22,True:11,}
    for item in info.values():
        print(item)

    8.方法

    (1)fromkeys

      根据序列,创建字典,并赋予统一的值

    v = dict.fromkeys(["k1","k2","k3"],"hahah")
    print(v)

    (2)get

      a.寻找指定key值的value,找不到报错

    info = {"k1":11,"k2":"hechouzi","k3":("lakers"),1:22,True:11,}
    v =info["k1"]
    print(v)

      b.寻找指定key值的value,找不到返回指定值,默认为None

    info = {"k1":11,"k2":"hechouzi","k3":("lakers"),1:22,True:11,}
    v =info.get("k111",11111)
    print(v)

    (3) pop 删除并获取值

      a.删除k1,并返回指定值

    info = {"k1":11,"k2":"hechouzi","k3":("lakers"),1:22,True:11,}
    v =info.pop("k1",11111)
    print(info,v)

      b.随机删除一个key和它对应的value

    info = {"k1":11,"k2":"hechouzi","k3":("lakers"),1:22,True:11,}
    k,v =info.popitem()
    print(k,v,info)

    (4)setdefault 设置值,已存在不设置并获取当前的value,不存在则设置并获取value

    info = {"k1":11,"k2":"hechouzi","k3":("lakers"),1:22,True:11,}
    v = info.setdefault("k1",123)
    print(info,v)

    (5) update 更新

    两种方法

    info = {"k1":11,"k2":"hechouzi","k3":("lakers"),1:22,True:11,}
    # info.update({"k1":123,"k2":"hahaha"})
    info.update(k1 = "lakers",k3=111,Ture=111)
    print(info)

    四、类的分类

    1.根据是否可变分为

    可变类型:列表、字典

    不可变类型:字符串、数字、元组

    2.根据访问顺序分为

    直接访问:数字

    顺序访问:字符串、列表、元组

    映射访问:字典

    3.存放元素个数

    容器类型:列表、元组、字典

    原子类型:数字、字符串

     

  • 相关阅读:
    Android学习—Shape
    Android学习—Application的作用
    用户管理
    DPKG命令与软件安装、APT
    Linux学习笔记
    C如何使用内存
    九九乘法表.py
    超市买苹果.py
    第一个程序python.py
    JavaScript一些备忘知识
  • 原文地址:https://www.cnblogs.com/hechengwei/p/8909407.html
Copyright © 2020-2023  润新知