0
点赞
收藏
分享

微信扫一扫

python中字典dict的方法fromkeys

北邮郭大宝 2022-11-05 阅读 152


该方法用于创建并返回一个新的字典.两个参数,第一个为字典的键,第二个为字典的值(默认为None).

示例:
1.

>>> dict = dict.fromkeys([1,2,3])
>>> dict
{1: None, 2: None, 3: None}



>>> dict = dict.fromkeys([1,2,3],'test')
>>> dict
{1: 'test', 2: 'test', 3: 'test'}



这里需要注意!!!(没你想的那么好)

>>> dict = dict.fromkeys([1,2,3],['one','two','three'])
>>> dict
{1: ['one', 'two', 'three'], 2: ['one', 'two', 'three'], 3: ['one', 'two', 'three']}


举报

相关推荐

0 条评论