1.两个列表转换为字典
list1=["a","b","c"]
list2=[1,2,3]
d={}
for i in range(len(list1)):
d[list1[i]]=list2[i]
print(d)
2. 使用内置函数 zip
list1=["a","b","c"]
list2=[1,2,3]
d=dict(zip(list1,list2))
print(d)
3.求一个列表中所有数据类型的次数
l = [5,3,"s",[6,78],{2:4},(5,7),set([8,6]),"b",-9,1.0]
d = {"int":0,"str":0,"float":0,"list":0,"tuple":0,"set":0,"dict":0,"complex":0}
for i in l:
if isinstance(i,str):
d["str"]+=1
if isinstance(i,int):
d["int"]+=1
if isinstance(i,float):
d["float"]+=1
if isinstance(i,list):
d["list"]+=1
if isinstance(i,set):
d["set"]+=1
if isinstance(i,tuple):
d["tuple"]+=1
if isinstance(i,dict):
d["dict"]+=1
if isinstance(i,complex):
d["complex"]+=1
print(d)
4. 字典转化成列表
d = {'a': 1, 'b': 2, 'c': 3}
key = list(d.keys())
print(key)
value = list(d.values())
print(value)
|