python-四大数据结构

    技术2023-05-07  92

    list(列表)

    list基本格式 #列表名 = [" "," "," "] nums = ["a",1,"b"] 遍历列表 names = ["李华", "小明", "马瑞"] for name in names: print(name) #执行结果: ''' 李华 小明 马瑞 ''' 增 a = [1,2] b = [3,4] #******append****** a.append(b) #将列表b内的每个元素,看做一个整体追加在列表a的最后面 print(a) #******extend****** a.extend(b) #将列表b内的每个元素,拆分开逐一追加在列表a的最后面 print(a) #******insert****** a.insert(1,5) #将元素5插入到列表a元素下标为1的位置 print(a) #执行结果: ''' [1, 2, [3, 4]] [1, 2, [3, 4], 3, 4] [1, 5, 2, [3, 4], 3, 4] ''' 删 nums = ["a","b","c",1,2,3] #******del****** del nums[2] #删除列表nums下标为2的元素 print(nums) #******pop****** nums.pop() #删除列表nums最后一个元素 print(nums) #******remove****** nums.remove("b") #删除指定内容的元素 print(nums) #执行结果: ''' ['a', 'b', 1, 2, 3] ['a', 'b', 1, 2] ['a', 1, 2] ''' 改 names = ["张三","李四"] print(names) names[0] = "小三" print(names) #执行结果: ''' ['张三', '李四'] ['小三', '李四'] ''' 查 names = ["张三","李四","张三"] name = input("请输入要查的元素:") if name in names: #index查询的元素有多个时,则输出第一个元素的下标 print("该元素存在,该元素的下标为:%d"%names.index(name)) else: print("该元素不存在") #count查询该元素在列表中出现的次数 print("该列表存在 %d 个%s"%(names.count(name),names[names.index(name)])) #执行结果: ''' 请输入要查的元素:张三 该元素存在,该元素的下标为:0 该列表存在 2 个张三 ''' 排序 nums = [1,2,3,6,5,4] print(nums) #******reverse****** nums.reverse() #逆序 print(nums) #******sort****** nums.sort() #升序 print(nums) nums.sort(reverse=True) #降序 print(nums) #执行结果: ''' [1, 2, 3, 6, 5, 4] [4, 5, 6, 3, 2, 1] [1, 2, 3, 4, 5, 6] [6, 5, 4, 3, 2, 1] ''' 枚举 nums = ["a","b","c","d"] for i,j in enumerate(nums): print(i,j) #执行结果: ''' 0 a 1 b 2 c 3 d ''' 嵌套列表(二位数组)基本格式 列表名 = [["",""],["",""],["",""]] print(列表名[外列表下标][内列表下表]) list列表实例 products = [["redmi k30",1499],\ ["redmi k30 pro",2699],\ ["redmi k30 pro变焦版",2999],\ ["redmi k30至尊纪念版",1999],\ ["redmi k30S至尊纪念版",2599],\ ["手机壳",20],\ ["钢化膜",10],\ ["OTG",8]] print("\n------商品列表------") #遍历商品列表和价格 for x,phones in enumerate(products): print(x+1,"%-20s"%phones[0],"%d元"%phones[1]) print("\n------选购环节------") #用户自行选购 shopping_cart = [] num = input("请输入需要添加进购物车商品的编号:") while num != "exit": if num.isdigit() and int(num)>0 and int(num)<=8: shopping_cart.append(products[int(num)]) print("请问还需添加其他商品到购物车吗?") print("若需添加请输入对应编号,结账请输入:exit") num = input("请输入需要添加进购物车商品的编号:") else: num = input("该商品不存在,请重新输入:") else: print("\n------结账环节------") money = 0 if len(shopping_cart) != 0: print("以下是您购物车内的商品:") for shopping in shopping_cart: print(shopping[0],":%d元"%shopping[1]) money = money + shopping[1] print("您本次共需支付:%d元"%money) else: print("购物车空空如也。") print("******感谢您的光临,下次再见~******") #执行结果: ''' ------商品列表------ 0: redmi k30 1499元 1: redmi k30 pro 2699元 2: redmi k30 pro变焦版 2999元 3: redmi k30至尊纪念版 1999元 4: redmi k30S至尊纪念版 2599元 5: 手机壳 20元 6: 钢化膜 10元 7: OTG 8元 ------选购环节------ 请输入需要添加进购物车商品的编号:3 请问还需添加其他商品到购物车吗? 若需添加请输入对应编号,结账请输入:exit 请输入需要添加进购物车商品的编号:5 请问还需添加其他商品到购物车吗? 若需添加请输入对应编号,结账请输入:exit 请输入需要添加进购物车商品的编号:exit ------结账环节------ 以下是您购物车内的商品: redmi k30至尊纪念版 :1999元 手机壳 :20元 您本次共需支付:2019元 ******感谢您的光临,下次再见~****** '''

    tuple(元组)

    tuple基本格式 #元组名 = (" "," "," ") tup = (123,) #单个元素时结尾加逗号 tup = (1,2,3) 遍历元组 和list列表操作相同 增删改查 增:只能通过连接2个不同的元祖生成一个新元组删:del 元组名删除整个变量改:元组元素不支持修改查:和list列表操作相同

    dict(字典)

    dict基本格式 #字典名 = {"":"", "":""} peoples = {"张三":18, "李四":15} 遍历字典 peoples = {"张三":18, "李四":15} for key,value in peoples.items(): print(key,value) #执行结果: ''' 张三 18 李四 15 ''' 增 peoples = {"张三":18, "李四":15} age = input("请输入年龄:") peoples["王五"] = age print(peoples) #执行结果: ''' 请输入年龄:17 {'张三': 18, '李四': 15, '王五': '17'} ''' 删 peoples = {"张三":18, "李四":15, "王五":17} del peoples["张三"] #删除指定的键值对 peoples.pop("李四") #和del作用相同 print(peoples) peoples.clear() #清空字典内的内容 print(peoples) del peoples #删除整个字典,删除后不可访问 #执行结果: ''' {'李四': 15} {} ''' 改 peoples = {"张三":18, "李四":15} peoples["张三"] = 17 print(peoples) #执行结果: ''' {'张三': 17, '李四': 15} ''' 查 peoples = {"张三":18, "李四":15} #******keys****** print(peoples.keys()) #得到所有键 #******values****** print(peoples.values()) #得到所有值 #******items****** print(peoples.items()) #得到所有键值对 #******get****** print(peoples.get("小三","该键不存在")) #查找指定键对应的值 #执行结果: ''' dict_keys(['张三', '李四']) dict_values([18, 15]) dict_items([('张三', 18), ('李四', 15)]) 该键不存在 ''' 其他操作 合并两个字典:dict1.update(dict2)把两个列表转换为字典:dict(zip(list1,list2))

    set(集合)

    set基本格式 #集合名 = {"", "", ""} nums = {"a", 1, "b", 2} set独有操作 s = {1010, "张三", 78, 9} t = {1010, "李四", 12.3, 1010} print(s - t) # 差集 print(s & t) # 交集 print(s ^ t) # 补集 print(s | t) # 并集 #执行结果 ''' {'张三', 9, 78} {1010} {'张三', 9, '李四', 12.3, 78} {1010, '张三', 9, '李四', 12.3, 78} '''

    小结对比

    NULL是否有序是否可变类型列表["",""]有序可变类型元祖("","")有序不可变类型字典{"":""}无序键不可变,值可变集合{"",""}无序可变类型(不重复)
    Processed: 0.012, SQL: 10