day7 字典元组容器类型基操

2018-11-13  本文已影响0人  遇见那天的雨真大

计算机基础随笔

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

is:判断地址是否相等

python数据存储:

1.列表赋值

1列表赋值

list1 = [1,2,3]
list2 = list1
list2.append(100)
print(list1)
list1 = [1,2,3]
list2 = list1[:]
list2.append(100)
print(list1)

2.列表相关操作方法

names = ["lorry","lorrybz","lorry123","lorry","小明","小明","小红","小明","狗蛋","二狗子",]
print(names.count("小明"))
names = ["lorry","小明","小红","小明","狗蛋","二狗子",]
names.extend([1,2])
print(names)
names.extend("world")
print(names)
names.extend(range(1,10))
print(names)
numbers = [1,2,3,4,3,5]
print(numbers.index(3))
num = [1,89,41561,76,88,88,66]
num.reverse()
print(num)
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)
names = ["lorry","小明","小红","小明","狗蛋","二狗子",]
names.clear()
print(names)

names = ["lorry","小明","小红","小明","狗蛋","二狗子",]
names = []
print(names)
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)

tuple1 = (1,"a",True,[1,"lorry"])
print(tuple1)
tuple2 = (1,)
print(tuple2)
tuple3 = 1,2,"lorry"
print(tuple3,type(tuple3))
tuple4 = (10,20)
print(tuple4[0],tuple4[-2])
可以通过变量个数和元组元素个数保持一致来获取元组中的每个元素
x,y = tuple4
print(x,y)
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)
tuple1 = (1,2,3)
listn = ["aa","bb","cc"]
print(*listn)
print(*tuple1)
tuple1 = 1,2,4,5
print(tuple1[1])
print(tuple1[0:3])
for item in tuple1:
    print(item)

+ * 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.查
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)
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)
上一篇下一篇

猜你喜欢

热点阅读