import random # Isbase=[] # for i in range(1,11): # Isbase.append(i) # random.shuffle(Isbase) # print(Isbase) dic={“黑”:123,“白”:456,“red”:897} 删 print(dic.pop(“黑”)) # 删除指定的键 并且返回 print(dic.popitem()) #根据键,删除指定的值 dic.clear() #清空字典的键值对 查 x=dic[“黑”] #根据键查询值 print(x) print(dic.get(“red”)) #get 如果key不存在返回None,或者指定的值 print(len(dic))# 键的总数 print(str(dic)) #输出字典 打印的字符串表示 print(dic.keys()) #以列表返回一个字典所有的键 print(dic.values()) # 以列表返回一个字典所有的值 print(dic.items()) #以列表返回可遍历的(键,值)元组数组 x="黑"in dic #判断键在字典存在? print(x) for key in dic: print(key,dic[key]) print(dic.copy()) #返回一个新的字典,内容一样,地址不同 print(dic.popitem()) set集合 set1={1,2,3,4,5,6,7,8,9} set2={0,8,9,4,5,6} print(set1.union(set2)) print(set1.intersection(set2)) print(set1.difference(set2)) set3=set1|set2 print(set3) print(set1.issubset(set2)) set1.remove(1) print(set1) set1.pop() print(set1) set1.clear() print(set1) set1.add(99) print(set1) set1.update(“wowlpl”) print(set1) 冒泡排序 ls=[1,2,7,8,99,66,2,144,8466] for i in range(0,len(ls)-1): # for j in range(0,len(ls)-i-1): # if ls[j] > ls[j + 1]: # ls[j],ls[j+1]=ls[j+1],ls[j] # print(ls) # 元组 冒泡排序 ls1=[(“学生1”,50),(“学生2”,30),(“学生3”,60)] for i in range(0,len(ls1)-1): for j in range(0,len(ls1)-i-1): if ls1[j][1] > ls1[j + 1][1]: ls1[j],ls1[j+1]=ls1[j+1],ls1[j] print(ls1) class student: name="" socre=0 s1=student() s1.name=“学生1” s1.socre=60 s2=student() s2.name=“学生2” s2.socre=30 s3=student() s3.name=“学生3” s3.socre=80 ls2=[s1,s2,s3] for i in range(0,len(ls2)-1): for j in range(0,len(ls2)-i-1): if ls2[j].socre<ls2[j+1].socre: ls2[j],ls2[j+1]=ls2[j+1],ls2[j] for item in ls2: print(item.name,item.socre) 选择排序 ls=[88,99,654,2,27895,13589] for i in range(0,len(ls)-1): for j in range(i+1,len(ls)): if ls[i]>ls[j]: ls[i],ls[j]=ls[j],ls[i] print(ls) ls=[1,5842,81,551,5] for i in range(0,len(ls)-1): for j in range(i+1,len(ls)): if ls[i]>ls[j]: ls[i],ls[j]=ls[j],ls[i] print(ls)