|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.dict()不是一个BIF,是一个工厂函数(类型);
- >>> dict1 = {} #定义一个空的字典
- >>> dict1.fromkeys((1, 2, 3)) #不提供第二个参数时,默认为None
- {1: None, 2: None, 3: None}
- >>> dict1.fromkeys((1, 2, 3), 'Number') #第二个参数设置为Number时
- {1: 'Number', 2: 'Number', 3: 'Number'}
- >>> dict1.fromkeys((1, 2, 3), ('one', 'two', 'three'))#第二个参数设置为('one', 'two', 'three')时
- {1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
- >>> dict1.fromkeys((1, 3), '数字') #重新创建了新的字典
- {1: '数字', 3: '数字'}
- >>> 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: '赞'}
- >>> for eachKey in dict1.keys():
- print(eachKey) #打印字典中所有的Key
-
- 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
- >>> for eachValue in dict1.values():
- print(eachValue) #打印字典中所有的Value
-
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- 赞
- >>> for eachItem in dict1.items():
- print(eachItem) #打印字典中所有的项Item
-
- (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, '赞')
- >>> print(dict1[31])
- 赞
- >>> print(dict1[32]) #程序报错
- Traceback (most recent call last):
- File "<pyshell#18>", line 1, in <module>
- print(dict1[32])
- KeyError: 32
- >>> dict1.get(32) #访问字典中不存在的项
- >>> print(dict1.get(32))
- None
- >>> dict1.get(32, '木有!') #访问字典中不存在的项
- '木有!'
- >>> dict1.get(31, '木有!') #访问字典中的项
- '赞'
- >>> 31 in dict1 #成员字典操作符,查找的是键而非值
- True
- >>> 32 in dict1 #成员字典操作符,查找的是键而非值
- False
- >>> 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: '赞'}
- >>> dict1.clear() #清空字典
- >>> dict1
- {}
- >>> dict1 = {}
- >>> a = {'姓名':'小甲鱼'}
- >>> b = a
- >>> b
- {'姓名': '小甲鱼'}
- >>> a = {} # a清空了
- >>> a
- {}
- >>> b # b未清空
- {'姓名': '小甲鱼'}
- >>> a = b
- >>> a
- {'姓名': '小甲鱼'}
- >>> b
- {'姓名': '小甲鱼'}
- >>> a.clear()
- >>> a # a清空
- {}
- >>> b # b清空
- {}
- >>> a = {1:'one', 2:'two', 3:'three'}
- >>> b = a.copy()
- >>> c = a
- >>> c
- {1: 'one', 2: 'two', 3: 'three'}
- >>> a
- {1: 'one', 2: 'two', 3: 'three'}
- >>> b
- {1: 'one', 2: 'two', 3: 'three'}
- >>> id(a)
- 38270128
- >>> id(b) #拷贝后的地址与原地址不一样
- 35828672
- >>> id(c) #赋值后的地址与原地址一样
- 38270128
- >>> c[4] = 'four' #c增加一个值
- >>> c
- {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
- >>> a
- {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
- >>> b
- {1: 'one', 2: 'two', 3: 'three'}
- >>> a.pop(2)
- 'two'
- >>> a
- {1: 'one', 3: 'three', 4: 'four'}
- >>> a.popitem() #随机从字典中去除一个元素
- (4, 'four')
- >>> a
- {1: 'one', 3: 'three'}
- >>> a.setdefault('小白') # 找不到对应值时,会自动进行添加
- >>> a
- {1: 'one', 3: 'three', '小白': None}
- >>> a.setdefault(5, 'five')
- 'five'
- >>> a
- {1: 'one', 3: 'three', '小白': None, 5: 'five'} #添加以后自动排序
- >>> b = {'小白':'狗'}
- >>> a.update(b) #利用一个字典或映射关系去更新另外一个字典
- >>> a
- {1: 'one', 3: 'three', '小白': '狗', 5: 'five'}
复制代码 |
评分
-
查看全部评分
|