shen1991 发表于 2018-5-3 15:48:11

fromkeys疑惑

>>> dict1={}
>>> dict1.fromkeys((1, 2, 3), ('one', 'two', 'three'))
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
>>> print(dict1)
{}

小甲鱼说fromkeys方法是创建一个新字典,那么新字典的名字是什么?我们怎么调用这个新字典?

Charles未晞 发表于 2018-5-3 15:50:25

dict1={}
a = dict1.fromkeys((1, 2, 3), ('one', 'two', 'three'))
print(a)
你想要的是这样的效果么。。。

shen1991 发表于 2018-5-3 15:55:42

Charles未晞 发表于 2018-5-3 15:50
你想要的是这样的效果么。。。

就是调用fromkeys方法时必须要指定新字典的名字,不指定就找不到是吧

Charles未晞 发表于 2018-5-3 15:59:00

shen1991 发表于 2018-5-3 15:55
就是调用fromkeys方法时必须要指定新字典的名字,不指定就找不到是吧

是的,不是 in-place操作。不会改变dict1。

shen1991 发表于 2018-5-3 16:00:20

Charles未晞 发表于 2018-5-3 15:59
是的,不是 in-place操作。不会改变dict1。

谢谢

wangzhezhixin 发表于 2018-5-3 16:29:04

请问这个函数的作用是什么?
页: [1]
查看完整版本: fromkeys疑惑