#encoding=utf-8
"""
把一个列表偶数位作为key,奇数位作为value,转换成字典
list1 = [1,2,3,4,5,6]
"""
def listToDict(list1):
wantDict={}
v_list=[]
k_list=[]
for i in range(len(list1)):
if i%2==0:
k_list.append(list1[i])
else:
v_list.append(list1[i])
for i in range(len(k_list)):
wantDict[k_list[i]]=v_list[i]
return wantDict
list1 = [1,2,3,4,5,6]
print(listToDict(list1))
"""
两个长度相同的list,一个里面的做字典的key,一个里面做字典的value,请写个函数实现。
"""
def twoListBeDict(L1,L2):
targetDict={}
for i in range(len(L1)):
if isinstance(L1[i],(list,dict)):
print("sorry,字典的key必须是不可变的")
break
else:
targetDict[L1[i]]=L2[i]
return targetDict
L1=[1,3,5,7]
L2=[1,4,6,8]
L3=[[1],[2],[3],[4]]
L4=[{1:'a'},2,3,4]
print(twoListBeDict(L1,L2))
print(twoListBeDict(L3,L2))
print(twoListBeDict(L4,L2))