|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>> d = {"吕布":"口口布", "关羽":"关习习"}
>>> type(d)
<class 'dict'>
>>> d
{'吕布': '口口布', '关羽': '关习习'}
>>> d["吕布"]
'口口布'
>>> # 使用 dict() 函数,跟 list()、tuple()、str() 类似,dict() 函数用来生成字典,它的每个参数就是一个键值对,键与值直接使用等号挂钩
>>> b = dict(吕布="口口布",关羽="关习习", 刘备="刘baby")
>>> b
{'吕布': '口口布', '关羽': '关习习', '刘备': '刘baby'}
>>> # 使用列表作为参数,列表中的每个元素是使用元组包裹起来的键值对:
>>> b = dict({"刘备":"刘baby","关羽":"关习习", "吕布":"口口布"})
>>> b
{'刘备': '刘baby', '关羽': '关习习', '吕布': '口口布'}
>>> c = dict([("吕布","口口布"), ("关羽","关习习"), ("刘备","刘baby")])
>>> c
{'吕布': '口口布', '关羽': '关习习', '刘备': '刘baby'}
>>> e = dict({"吕布":"口口布", "刘备":"刘baby"}, 关羽="关习习")
>>> e
{'吕布': '口口布', '刘备': '刘baby', '关羽': '关习习'}
>>> f = dict(zip(["吕布","关羽","刘备"], ["口口布","关习习","刘baby"]))
>>> f
>>> {'吕布': '口口布', '关羽': '关习习', '刘备': '刘baby'}
>>> d = dict.fromkeys("FishC", 250)
>>> d
{'F': 250, 'i': 250, 's': 250, 'h': 250, 'C': 250}
>>> d = dict.fromkeys("FishC")
>>> d
{'F': None, 'i': None, 's': None, 'h': None, 'C': None}
>>> d['F'] = 70
>>> d
{'F': 70, 'i': None, 's': None, 'h': None, 'C': None}
>>> d['D'] = 67
>>> d
{'F': 70, 'i': None, 's': None, 'h': None, 'C': None, 'D': 67}
>>> d.pop('s')
>>> d
{'F': 70, 'i': None, 'h': None, 'C': None, 'D': 67}
>>> d.pop('欧冠')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: '欧冠'
>>> d.pop('欧冠', '没有')
'没有'
>>> d.popitem()
('D', 67)
>>> d
{'F': 70, 'i': None, 'h': None, 'C': None}
>>> del d
>>> d
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'd' is not defined
>>> d = dict.fromkeys("FishC", 250)
>>> d
{'F': 250, 'i': 250, 's': 250, 'h': 250, 'C': 250}
>>> d.clear()
>>> d
{} |
|