字典的fromkeys方法的使用

fromkeys

1、将列表创建为字典
2、创建的字典value等于将值拷贝了三次,实际value指向的是一个地址
 

 

a=info.fromkeys([1,2,3])#将列表创建为字典
print(a)

b=dict.fromkeys([4,5,6],[1,{'name':'Alan'},678])#等于将值拷贝了三次,实际value指向的是一个地址
print(b)
b[4][1]['name']='chen sir'#
print(b)#fromkeys 只适合创建一层的字典,

  

 



原文地址:https://www.cnblogs.com/pythonkids/p/7667739.html