python中字典遍历四种和方法"""
dec = {}
dec[1] = "Liu"
dec[2] = "op"
dec[3] = "ui"
for i in dec:
print("1 -%d -> %s" % (i, dec[i]))
for j in dec.keys():
print("2-%d -> %s" %(j, dec[j]))
for k in dec.values():
print("3-%s -> %s" %(k, "y"))
for m,n in dec.items():
print((m,n))
'''python中查找字典元素方法'''
#通过values取key
print(list(dec.keys())[list(dec.values()).index("ui")])
#通过key取values
print(dec[3])
#修改对应key下的values
dec[1] = "Modify"
print(dec[1])
#修改对用value的key值
'''dict.pop(values)然后结合dict.update()\重新创建\del'''
keys = list(dec.keys())[list(dec.values()).index("ui")]
dec[4] = dec.pop(keys)
keys1 = list(dec.keys())[list(dec.values()).index("Modify")]
dec.update({"d":dec.pop(keys1)})
keys2 = list(dec.keys())[list(dec.values()).index("op")]
print(dec.keys())
'''set数据类型CRUD set数据类型的意义在于提供一种直接批量处理数据的方式'''
set1 = set(dec) #使用dict进行初始化
set2 = {4,5,6} #直接使用{}加入数据,若直接使用{}申请空set,系统默认{}为dict
set3 = set()
set3.add(7) #每次只能add一个元素
set3.add((1,2)) #可以插入tuple,不能插入list,只能放入不可变对象
set4 = set(["Liu", "op", "ui"]) #但是可以使用可变对象初始化
print(list(set3 | set2)) #求并集
print(set3.union(set2))
print(tuple(set4 & set3)) #求交集
print(set4.intersection(set3))
bool_t = isinstance((1,2), collections.Iterable) #tuple is iterable
bool_t = isinstance({1,2}, collections.Iterable) #set is also iterable object
bool_t = isinstance([1,2], collections.Iterable) #list is also
bool_t = isinstance({1:"Liu",2:"You"}, collections.Iterable) #list is also
print(bool_t)
#enumerate() function makes every iterable object disposing with numnber way
for i,value in enumerate(["A", "B", "C", "D"]):
print(i,value)
for i,value in enumerate(("A", "B", "C", "D")):
print(i,value)
for i,value in enumerate({"A", "B", "C", "D"}):
print(i,value)
for i,value in enumerate({"A":"y", "B":"x", "C":"z", "D":"w"}): #dict through processing of enumerate(),disposing with keys
print(i,value)