美文网首页
day7 字典元组容器类型基操

day7 字典元组容器类型基操

作者: 遇见那天的雨真大 | 来源:发表于2018-11-13 23:21 被阅读0次

    计算机基础随笔

    == :判断两个数据的值是否相等

    is:判断地址是否相等

    python数据存储:

    • 1.给变量赋值的时候,如果数据的类型是数字或者字符串,不会直接开辟空间存数据。
      而是现在数字字符串对应的缓存区去查看是否已经存储对应的数据,如果已经存了,直接将之前得数据对
      应的地址赋给存的变量,如果没有开辟空间存储数据。
      其他类型的数据,都是直接开辟空间存储数据然后再把地址返回。
    • 2.容器类型中的元素,在容器中不是直接存的值,而是存的对应的地址

    1.列表赋值

    1列表赋值

    • a.直接使用一个列表变量 给另一个列表变量赋值,赋的是地址。赋完值之后,对其一个列表进行增删改,会影响另外一个列表。
    list1 = [1,2,3]
    list2 = list1
    list2.append(100)
    print(list1)
    
    • b.如果赋值的时候,赋的是列表的切片或者拷贝,会产生新的地址,然后使用新的地址赋值,赋完值之后,两个列表之间不影响。
    list1 = [1,2,3]
    list2 = list1[:]
    list2.append(100)
    print(list1)
    

    2.列表相关操作方法

    • a.列表.count(元素) - 获取指定元素在列表中出现的次数
    names = ["lorry","lorrybz","lorry123","lorry","小明","小明","小红","小明","狗蛋","二狗子",]
    print(names.count("小明"))
    
    • b.列表1.extend(元素) - 将序列中所有的元素都添加到列表中
    names = ["lorry","小明","小红","小明","狗蛋","二狗子",]
    names.extend([1,2])
    print(names)
    names.extend("world")
    print(names)
    names.extend(range(1,10))
    print(names)
    
    • c:index.- 获取指定元素下标的下标
      注意:如果元素有多个,只取第一个下标。
      如果元素不存在,会报错(valueerro)
    numbers = [1,2,3,4,3,5]
    print(numbers.index(3))
    
    • d:列表reverse- 反向列表、(将列表倒序数)
    num = [1,89,41561,76,88,88,66]
    num.reverse()
    print(num)
    
    • e:列表.sort- 对列表进行升序排序)
      num.sort(reverse = True)对列表进行从到大小
      注意:列表的元素类型必须一样,元素支持比较运算符
    num = [1,89,41561,76,88,88,66]
    num.sort()
    print(num)
    num = [1,89,41561,76,88,88,66]
    num.sort(reverse = True)
    print(num)
    names = ["lorry","小明","小红","小明","狗蛋","二狗子",]
    names.sort()
    print(names)
    
    • f:列表.clear()- 清空列表.# 清空列表尽量使用clear
    names = ["lorry","小明","小红","小明","狗蛋","二狗子",]
    names.clear()
    print(names)
    
    names = ["lorry","小明","小红","小明","狗蛋","二狗子",]
    names = []
    print(names)
    
    • h:列表.copy- 将列表中元素直接赋值产生一个新的列表,和列表[:]效果一样
      这儿的拷贝属于浅拷贝
    names = ["lorry","小明","小红","小明","狗蛋","二狗子",]
    names2 = names.copy()
    print(id(names),id(names2))
    

    3.深浅拷贝

    copy.copy(对象) -浅拷贝 (直接拷贝元素的值,产生一个新的地址)
    copy.deepcopy(对象) -深拷贝 (不会直接复制地址,而是将地址对应的值拷贝一份,产生新的地址。)*
    对拷贝的列表进行操作,不会影响原来的列表。

    浅拷贝

    list1 = [1,2,3]
    list2 = [4,5,6]
    list3 = [list1,list2]
    list4 = list3.copy()
    list4.append(5)
    print("***")
    print(list3)
    print(list4)
    

    对元素对应的值进行操作,会影响拷贝前的列表。

    list1 = [1,2,3]
    list2 = [4,5,6]
    list3 = [list1,list2]
    list4 = list3.copy()
    list4[0].append(1010000)  #对列表中元素的操作会进行改变
    print(list3)
    print(list4)
    

    深拷贝

    直接拷贝地址对应的值,产生新的地址,无论怎样改变都不会变化。

    list1 = [1,2,3]
    list2 = [4,5,6]
    list3 = [list1,list2]
    list4 = copy.deepcopy(list3)
    list3.append(5)
    print("深拷贝list3",list3)
    print("深拷贝list4",list4)
    
    list1 = [1,2,3]
    list2 = [4,5,6]
    list3 = [list1,list2]
    list4 = copy.deepcopy(list3)
    list4.append(5)
    print("*-**")
    print("深拷贝list3",list3)
    print("深拷贝list4",list4)
    
    list1 = [1,2,3]
    list2 = [4,5,6]
    list3 = [list1,list2]
    list4 = copy.deepcopy(list3)
    list3[0].append(100)
    print("-----")
    print("深拷贝list3",list3)
    print("深拷贝list4",list4)
    list1 = [1,2,3]
    list2 = [4,5,6]
    list3 = [list1,list2]
    list4 = copy.deepcopy(list3)
    list4[0].append(100)
    print("++++")
    print("深拷贝list3",list3)
    print("深拷贝list4",list4)
    

    4.元组 (tuple)

    • 1.什么是元组 (tuple)元组就是不可变的列表(有序不可变)
      有序 - 可以通过下标获取元素不可变 - 不支持增删改
    • 2.元组的字面量:()小括号将多个元素括起来,多个元素之间用 , 号隔开.
      • a.只有一个元素的元组要加 ","
      • b,tuple3 = 1,2,"lorry" 可以省掉括号
    tuple1 = (1,"a",True,[1,"lorry"])
    print(tuple1)
    tuple2 = (1,)
    print(tuple2)
    tuple3 = 1,2,"lorry"
    print(tuple3,type(tuple3))
    
    • 3获取元组元素,
    tuple4 = (10,20)
    print(tuple4[0],tuple4[-2])
    可以通过变量个数和元组元素个数保持一致来获取元组中的每个元素
    x,y = tuple4
    print(x,y)
    
    • 4通过在变量前加,获取没有的变量获取到的元素剩下的部分,以列表形式返回
    tuple5 = ("lorry",98,88,97,78)
    name, *scores = tuple5
    print(name,scores)
    tuple5 = ("lorry",98,88,97,78)
    name,num,*scores = tuple5
    print(name,num)
    print(scores)
    *list1,num = tuple5
    print("===",list1,num)
    num1,*list1,num = tuple5
    print("===",num1,list1,num)
    
    • 5取元组和列表中的个数每个数
    tuple1 = (1,2,3)
    listn = ["aa","bb","cc"]
    print(*listn)
    print(*tuple1)
    
    • 6获取元组中的元素
    tuple1 = 1,2,4,5
    print(tuple1[1])
    print(tuple1[0:3])
    for item in tuple1:
        print(item)
    
    • 7 # 4 相关运算和列表运算

    + * in not in len() max() min() tuple

    print((1,2)+("lorry"))
    print((1,2)*3)
    print(1 in (1,2))
    print(len((1,2)))
    print(max((1,58,90)))
    

    5.元组相关的方法,只有count 和 index

    4.字典(dict)

    1.什么是字典(dict)

    字典是python内置的容器类的数据类型,可变,无序的。****字典的元素是键值对

    2.字典的字面量{键:值}大括号里面是键值对,多个键值对里面 , 号隔开

    键值对:(键:值)
    键 - 不可变的数据;唯一的,一般使用字符串作为key
    值 - 没有要求

    dict0 = {(1,2):20,"aa":20}
    dict1 = {"name":"lorry","age":23,"hobby":["girls","spa"]}
    print(dict0)
    print(dict1)
    dict0 = {(1,2):20,"aa":20,"aa":200}
    print(dict0)
    dict1 = {"name":"lorry","age":23,"hobby":["girls","spa"]}
    print(dict1["name"])
    print(list(dict1.keys()))
    print(list(dict1.values()))
    print(list(dict1.items()))
    dict1.update(dict0)
    print(dict1)
    

    什么时候使用字典:如果一个容器里面存储的数据是不同意义的数据(数据之间要区分)

    dict1 = {"name":"lorry","age":23,"hobby":["girls","spa"]}
    dict1["name"] = "lorrbz"
    print(dict1)
    back = dict1.pop("name")
    print(dict1)
    dict1 = {"name":"lorry","age":23,"hobby":["girls","spa"]}
    aa = dict1.popitem()
    print(aa)
    print(dict1)
    print("name" in dict1)
    

    3字典的增删改查

    1.查
    • a.获取单个值
      字典[key] - 获取字典中key的值 如果key不存在,会报错。
      字典.get() - 获取字典中key对应的值,如果key不存在,则会返回空值none
      none 是Python中关键字,表示一个特殊值(没有,空的意思)
    dict1 = {"name":"lorry","age":23,"hobby":["girls","spa"]}
    print(dict1["name"],dict1.get("age"))
    print(dict1["hobby"])
    print(dict1.get("sex"))
    print("name" in dict1)
    
    • b.遍历 : 直接遍历字典拿到的是所有的key
    dict1 = {"name":"lorry","age":23,"hobby":["girls","spa"]}
    for key in dict1:
        print(key)
    # 同时获取key和value
    for key,value in dict1.items():
        print(key,value)
    for i in dict1.items():
        print(i)
    
    2. 增(添加键值对)

    字典[key] = 值 当key不存在的时候,就是在字典中添加键值对

    dict1 = {"name":"lorry","age":23,"hobby":["girls","spa"]}
    dict1["特长"] = "腿毛"
    print(dict1)
    
    3 更新。字典1.update(序列) - 将序列中的元素转换成键值对,然后再添加多字典1中

    当key值有重名的时候,会用序列中键值对应的值,更新原字典的key对应的值
    注意:这儿的序列要求是能够转换成字典的序列,序列中的元素是只有两个元素的序列

    dict2 = {"a":"lorry","age":23}
    dict3 = {"aa":"lorrybx","agea":2200}
    dict2.update(dict3)
    print(dict2)
    dict3.update([[1,2],["aa",10000000]])
    print("******",dict3)
    dict2 = {"a":"lorry","age":23}
    dict2.update([(1,2),("aaa",500),["lorry","sex"]])
    print("====",dict2)
    
    av_catalog = {
        "欧美":{
            "www.youporn.com": ["很多免费的,世界最大的","质量一般"],
            "www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"],
            "letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"],
            "x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"]
        },
        "日韩":{
            "tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"]
        },
        "大陆":{
            "1024":["全部免费,真好,好人一生平安","服务器在国外,慢"]
        }
    }
    av_catalog["欧美"]["www.youporn.com"][0] = "高清无码"
    print(av_catalog)
    
    4.删

    a.del 字典[key] - 删除字典中key对应的键值对
    b.字典.pop(key) 去除字典中key对应的值

    dict1 = {"name":"lorry","age":23,"hobby":["girls","spa"]}
    del dict1["name"]
    print(dict1)
    dict1 = {"name":"lorry","age":23,"hobby":["girls","spa"]}
    back = dict1.pop("age")   #删除键值对拿到的值,不信你打印
    print(dict1)
    print(back)
    dict1 = {"name":"lorry","age":23,"hobby":["girls","spa"]}
    print("***",dict1.keys())
    back2 = dict1.popitem()
    print(dict1,back2)  # 取出最后一个值
    

    5字典的运算

    1.in 和 not in

    key in 字典 - 判断字典中是否存在指定的key

    dict1 = {"name":"lorry","age":23,"hobby":["girls","spa"]}
    print("name" in dict1)
    

    相关文章

      网友评论

          本文标题:day7 字典元组容器类型基操

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