方法一:循环
# 方法一: def method_one(): a = ["1", "2"] b = {} b.update({a[0]: a[1]}) print(b) def method_ones(): a = ["name", "zhangsan", "age", "18"] b = {} for i in range(0, len(a), 2): b[a[i]]= a[i+1] print(b)
执行结果:
方法二:zip函数
# 方法二:zip函数 def method_two(): a = ["name", "zhangsan", "age", "18"] # 偶数位 key,奇数位 value b = dict(zip(a[0::2], a[1::2])) print(b)
执行结果:
方法三:enumerate函数
# 方法三:enumerate函数 def method_three(): a = ["name", "lisi", "age", "21"] b = {} for index, item in enumerate(a): print(index, item) if index % 2 == 0: b[item] = a[index+1] print(b)
执行结果:
方法1:循环
# 方法一:循环 def method_one(): a = ["name", "age", 'addr'] b = ["1", "2", "beijing"] d = {} if len(a) == len(b): for i in range(len(a)): # d[a[i]] = b[i] d.update({a[i]: b[i]}) print(d)
执行结果: