python之函数用法fromkeys()

# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#python之函数用法fromkeys()



#fromkeys()
#说明:用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值
'''
>>> help(dict.fromkeys)
Help on built-in function fromkeys:

fromkeys(...)
    dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
    v defaults to None.
'''


#案例
d=[1,2,3]
dict={}
dict=dict.fromkeys(d)
print dict                          #{1: None, 2: None, 3: None}
dict=dict.fromkeys(d,'xiaodeng')    #xiaodeng为默认值
print dict                          #{1: 'xiaodeng', 2: 'xiaodeng', 3: 'xiaodeng'}

 

posted @ 2015-11-07 23:16  Xiao|Deng  阅读(14820)  评论(0编辑  收藏  举报