字典的方法

fromkeys(S[,v])  方法:   创建并返回新的字典

New dict with keys from S and values equal to v(v defaults to None).

eg:

>>>dict1 = {}

>>>dict1.fromkeys((1,2,3))    #创建只有键的字典,value默认为None

1: None, 2: None, 3: None}

>>>dict1.fromkeys((1,2,3),'Number')    #每一个value都是‘Number’

{1: 'Number', 2: 'Number', 3: 'Number'}

>>>dict1.fromkeys((1,2,3),('one','two','three'))    #不会一一对应

{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}

>>>dict1.fromkeys((1,3),'shuzi')    #重新建立一个新的字典dict1 

{1: 'shuzi', 3: 'shuzi'}

#------------------------------------------------------------------------------------------------------------#

.clear() :  清除字典内容,比给字典赋空要好

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


>>> a = {'name':'abc'}
>>> b = a
>>> a = {}
>>> b
{'name': 'abc'}
>>> a = b
>>> a.clear()
>>> b
{} 

 

posted @ 2018-03-11 22:12  我们分头打钱!  阅读(121)  评论(0编辑  收藏  举报