字典的方法

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
{} 

 

转载于:https://www.cnblogs.com/wawawawa-briefnote/p/8546053.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值