目录
3.get(),keys()和values(),items()
1.元组
介绍元组
⼀个元组可以存储多个数据,元组内的数据是不能修改的。元组又被称为只读列表。
定义元组 : 元组特点,定义元组使⽤⼩括号,且逗号隔开各个数据,数据可以是不同的数据类型。
# 定义一个元组 tuple 元组是不可变的数据类型 是不能更改的 但是可以使用 t1 = (10, 24 , 57) print(t1 , type(t1)) # 定义只有一个元组的元素 必须后面跟一个逗号 t2 = (19,) print(t2, type(t2)) # 假如说 不加逗号 则里面是神马数据类型 整个变量就是什么类型 变成元组需要加逗号 , t3 = ("hello",) print(t3 , type(t3))
2.字典
-
创造字典
- 字典⾥⾯的数据是以键值对形式出现,字典数据和数据顺序没有关系,即字典不⽀持下标,后期⽆论数据如何变化,只需要按照对应的键的名字查找数据即可。
# 字典的定义 dict表示字典的意思 dict1 = {} dict2 = dict() print(dict1,type(dict1)) print(dict1,type(dict2)) # 使用大括号 然后以逗号分割 键值对之间 使用冒号 # 1.没有顺序 没有办法通过下标 进行查找 只能通过键 进行查找 dict3 = {"name": "舔狗哥","age":20,"gender":"man"} print(dict3,type(dict3))
-
字典的操作
-
dict1 = {"name":"范帅","age":18,} print(dict1,type(dict1)) # 给字典添加一对键值对 dict1["gender"] = "man" print(dict1,type(dict1)) # 删除键值对 del dict1["gender"] print(dict1, type(dict1)) # 清空键值对 clear dict1.clear() print(dict1, type(dict1)) # 查找 dict2 = {"name":"哥哥","age":25} print(dict2["name"]) # 查找指定的键 对应的值 # print(dict2["gender"]) 查找没有的键 则报错 print(dict2.get("age")) print(dict2.get("id")) # 查找没有的键 则返回None print(dict2.get("id",110)) # 查找没有的键 则返回默认值 # 查找所有的键 dict3 = {"name":"Simon","age":36,"height":138} print(dict3.keys(),type(dict3.keys())) #查找所有的键 组成一个列表 print(dict3.values(),type(dict3.keys())) #查找所有的值 组成一个列表 # 查找所有的键值对 组成一个列表 里面是每一对键值对都是元组 print(dict3.items(),type(dict3.items())) print("===================================") # 字典的遍历 # 遍历的是字典的键 for key in dict3.keys(): print(key) print("==========================") # 遍历的是字典的值 for value in dict3.values(): print(value) print("=======================") # 遍历字典所有的键值 for k, v in dict3.items(): print(k, v)
3.get(),keys()和values(),items()
get():字典序列.get(key, 默认值)
keys():keys() 函数以列表返回一个字典所有的键。
values():values() 函数以列表返回一个字典所有的值。
items(): items() 函数以列表返回可遍历的(键, 值) 元组数组。
4.集合
-
集合的介绍
-
items() 函数以列表返回可遍历的(键, 值) 元组数组。
-
# 集合是大括号定义 是一个序列 # 定义一个空集合 set1 = set() print(set1, type(set1)) # 定义一个集合 使用逗号 隔开 set3 = {} print(set3 , type(set3)) # 定义一个集合 使用逗号 隔开 set2 = {1 ,3 ,4} print(set2, type(set2))
-
创建集合
- 创建集合使⽤ {} 或 set() , 但是如果要创建空集合只能使⽤ set() ,因为 {} ⽤来创建空字典。
set1 = {1, 3, 5} print(set1) # 增加数据 到集合中 set1.add("张三") print(set1) # set1.add([1, 2]) # print(set1) # update 表示更新数据 集合的特点之一 不能有重复数据 可以作为序列的去重工具 set1.update([1, 2]) print(set1) # 会将字符串abc拆开进入到 集合中 且发现 集合是没有顺序的 set1.update("abc") print(set1) # 删除数据 set1.remove("张三") print(set1) # set1.remove("李四") # print(set1) # 删除不存在的值报错 # set1.remove("李四") # print(set1) set2 = {1,2,"张飞"} set2.discard(1) print(set2) # 删除不存在的值不报错 set2.discard("陈浩") print(set2) set3 = {5,2,3,4,5} result = set3.pop() print(result)
集合的常见操作
-
5.公共操作
-
# len 方法 # 字符串 str1 = "hello do you hava dinner" print(len(str1)) # 列表 list1 = ["No","Or","Yes"] print(len(list1)) # 3.元组 t1 = ("老虎","鸡鸡","猴子") print(len(t1)) # 4.集合 set1 = {"谷爱凌","苏余名"} print(len(set1)) # 5.字典 dict1 = {"冬奥吉祥物":"冰墩墩","残奥辉吉祥物":"雪荣荣"} print(len(dict1)) # 删除操作 del # 1.字符串 str2 = "abcd" # del str2 # print(str2) # 2.列表 list2 = ["神殿","祭祀","弓箭"] del list2[0] print(list2) # 最大值操作 list3 = [21 , 23 , 20 ,21,26] print(max(list3)) # 最小值操作 print(min(list3)) # range 要前不要后 for i in range(1, 11) : print(i) for j in range(1,11,2) : print(j) print("====================================") list4 = ["吴豪","嘻嘻","凯凯","涛哥"] for i , j in enumerate(list4) : print(i , j) print(f"{j}是我们班级的第{i+1}帅") # 列表转换为元组 list5 = [1,2,3] print(tuple(list5),type(tuple(list5))) # 元组转换成列表 tuple1 = (1 , 2 , 3) print(list(tuple1), type(tuple1)) # 列表转集合 set4 = set(list5) print(set4 , type(set4))