|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 游戏小虾米 于 2017-7-16 00:38 编辑
Tip:
一,理论
二,应用
>>> dict1 ={}
>>> dict1.fromkeys((1,2,3)) # fromkeys方法
{1: None, 2: None, 3: None}
>>> dict1.fromkeys((1,2,3),"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')}
1 keys
>>> dict1 = dict1.fromkeys(range(32),'赞')
>>> dict1
{0: '赞', 1: '赞', 2: '赞', 3: '赞', 4: '赞', 5: '赞', 6: '赞', 7: '赞', 8: '赞', 9: '赞', 10: '赞', 11: '赞', 12: '赞', 13: '赞', 14: '赞', 15: '赞', 16: '赞', 17: '赞', 18: '赞', 19: '赞', 20: '赞', 21: '赞', 22: '赞', 23: '赞', 24: '赞', 25: '赞', 26: '赞', 27: '赞', 28: '赞', 29: '赞', 30: '赞', 31: '赞'}
012345678910111213141516171819202122232425262728293031
>>> for eachKey in dict1.keys():
print(eachKey, end = ' ')
2 values
>>> for eachValue in dict1.values():
print(eachValue, end = " ")
赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞 赞
3 items
>>> for eachItem in dict1.items():
print(eachItem, end = " ")
(0, '赞') (1, '赞') (2, '赞') (3, '赞') (4, '赞') (5, '赞') (6, '赞') (7, '赞') (8, '赞') (9, '赞') (10, '赞') (11, '赞') (12, '赞') (13, '赞') (14, '赞') (15, '赞') (16, '赞') (17, '赞') (18, '赞') (19, '赞') (20, '赞') (21, '赞') (22, '赞') (23, '赞') (24, '赞') (25, '赞') (26, '赞') (27, '赞') (28, '赞') (29, '赞') (30, '赞') (31, '赞')
4 get
>>> dict1.get(32, '木有') #没有键值,直接打印第二个参数,有键值,直接返回相应的值
'木有'
>>> dict1.get(31, '木有')
'赞'
5 成员操作符 in, not in
>>> 31 in dict1
True
>>> 32 in dict1
False
6 clear()
>>> dict1.clear() #清除方法, 用dict1 = {} 有隐患,直接使用clear()
>>> dict1
{}
7 update() #更新其他字典
8 setdefaults() # 添加字典文件
9 pop() 弹出某一个索引及其vuale
9 popitem() 随机弹出一组key+value
三,课后练习 |
评分
-
查看全部评分
|