首页 > 编程语言 > 详细

python中字典dict的方法fromkeys

时间:2019-04-26 22:28:19      阅读:99      评论:0      收藏:0      [点我收藏+]

该方法用于创建并返回一个新的字典.两个参数,第一个为字典的键,第二个为字典的值(默认为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'}
  1. 这里需要注意!!!(没你想的那么好)
>>> dict = dict.fromkeys([1,2,3],['one','two','three'])
>>> dict
{1: ['one', 'two', 'three'], 2: ['one', 'two', 'three'], 3: ['one', 'two', 'three']}

python中字典dict的方法fromkeys

原文:https://www.cnblogs.com/w-j-c/p/10776566.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!