| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
dict1={} 
dict1.fromkeys((1, 2, 3), ('one', 'two', 'three')) 
print(dict1) 
求助为什么打印结果还是空列表而不是{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
 本帖最后由 昨非 于 2020-11-19 22:06 编辑 
fromkeys 方法只用来创建新字典,不负责保存。当通过一个字典来调用 fromkeys 方法时, 
如果需要后续使用一定记得给他复制给其他的变量。 
- >>> dict1={}
 
 - >>> dict1.fromkeys((1,2,3),'number')
 
 - {1: 'number', 2: 'number', 3: 'number'}
 
 - >>> print(dict1)
 
 - {}
 
 - >>> dict2=dict1.fromkeys((1,2,3),'number')
 
 - >>> dict2
 
 - {1: 'number', 2: 'number', 3: 'number'} 
 
  复制代码 
- dict1={}
 
 - dict2=dict1.fromkeys((1, 2, 3), ('one', 'two', 'three'))
 
 - print(dict2)
 
  复制代码 
输出结果就是: - {1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
 
  复制代码 
 
 
 |   
 
 
 
 |