>>> ["改"]
['改']
>>> d = dict.fromkeys("FishC")
>>> d
{'F': None, 'i': None, 's': None, 'h': None, 'C': None}
>>> d["s"] = 115
>>> d
{'F': None, 'i': None, 's': 115, 'h': None, 'C': None}
>>> ["update([other]) 可以支持同时给它传入多个键值对,也可以直接给他传入另外一个字典,或者一个包含键值对的可迭代对象"]
['update([other]) 可以支持同时给它传入多个键值对,也可以直接给他传入另外一个字典,或者一个包含键值对的可迭代对象']
>>> d.update({"i":105,"h":104})
>>> d
{'F': None, 'i': 105, 's': 115, 'h': 104, 'C': None}
>>> d.update("F" = 70,"C "= 67)
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
>>> d.update(F ='70',C = '67')
>>> d
{'F': '70', 'i': 105, 's': 115, 'h': 104, 'C': '67'}
>>> d['C']
'67'
>>> d["c"]
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
d["c"]
KeyError: 'c'
>>> ["get(key[,default]) 方法,它可以存入一个default 参数,指定当找不到键的时候返回的值"]
['get(key[,default]) 方法,它可以存入一个default 参数,指定当找不到键的时候返回的值']
>>> d.get('c',"这里没有c")
'这里没有c'
>>> ["setdefault(key[,default]),当我们需要查找一个键是否存在于字典中,如果在,则返回它对应的值,如果不在,则给它指定一个新的值"]
['setdefault(key[,default]),当我们需要查找一个键是否存在于字典中,如果在,则返回它对应的值,如果不在,则给它指定一个新的值']
>>> d.setdefault('c','code')
'code'
>>> d.setdefault('C','code')
'67'
>>> ['items() keys() values() 这三个分别用于获取字典的键值对、键和值三者的视图对象 视图对象即字典的动态视图,这就意味着当字典的内容发生改变的时候,视图对象的内容也会相应地跟着改变。']
['items() keys() values() 这三个分别用于获取字典的键值对、键和值三者的视图对象 视图对象即字典的动态视图,这就意味着当字典的内容发生改变的时候,视图对象的内容也会相应地跟着改变。']
>>> d
{'F': '70', 'i': 105, 's': 115, 'h': 104, 'C': '67', 'c': 'code'}
>>> keys = d.keys()
>>> values = d.values()
>>> items = d.items()
>>> items
dict_items([('F', '70'), ('i', 105), ('s', 115), ('h', 104), ('C', '67'), ('c', 'code')])
>>> keys
dict_keys(['F', 'i', 's', 'h', 'C', 'c'])
>>> valuse
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
valuse
NameError: name 'valuse' is not defined
>>> values
dict_values(['70', 105, 115, 104, '67', 'code'])
>>> d.pop('c')
'code'
>>> items
dict_items([('F', '70'), ('i', 105), ('s', 115), ('h', 104), ('C', '67')])
>>> keys
dict_keys(['F', 'i', 's', 'h', 'C'])
>>> values
dict_values(['70', 105, 115, 104, '67'])
>>> e = d.copy()
>>> e
{'F': '70', 'i': 105, 's': 115, 'h': 104, 'C': '67'}
>>> ['copy() 也可以用于字典的浅拷贝']
['copy() 也可以用于字典的浅拷贝']
>>> len(d)
5
>>> 'C' in d
True
>>> 'c' not in d
True
>>> list(d)
['F', 'i', 's', 'h', 'C']
>>> lisy(d.values)
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
lisy(d.values)
NameError: name 'lisy' is not defined
>>> list(d.values)
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
list(d.values)
TypeError: 'builtin_function_or_method' object is not iterable
>>> list(d.values)
Traceback (most recent call last):
File "<pyshell#40>", line 1, in <module>
list(d.values)
TypeError: 'builtin_function_or_method' object is not iterable
>>> list(d.values())
['70', 105, 115, 104, '67']
>>> e = iter(d)
>>> next(e)
'F'
>>> next(e)
'i'
>>> next(e)
's'
>>> next(e)
'h'
>>> next(e)
'C'
>>> next(e)
Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
next(e)
StopIteration
>>> list(reversed(d.values()))
['67', 104, 115, 105, '70']
>>> ['嵌套']
['嵌套']
>>> d = {'吕布':{'语文':60,'数学':70,'英语':80},'小关关':{'语文':80,'数学':90,'英语':70}}
>>> d
{'吕布': {'语文': 60, '数学': 70, '英语': 80}, '小关关': {'语文': 80, '数学': 90, '英语': 70}}
>>> d['吕布']['数学']
70
>>> d = {'吕布':[60,70,80],'小关关':[80.90,70]}
>>> d['吕布'][1]
70
>>> ['上述即为字典嵌套序列']
['上述即为字典嵌套序列']
>>> d = {'F':70,'i':105,'s':115,'h':104,'C':67}
>>> b = {v:k for k,v in d.items()}
>>> b
{70: 'F', 105: 'i', 115: 's', 104: 'h', 67: 'C'}
>>> c = {v:k for k,v in d.items() if v > 100}
>>> c
{105: 'i', 115: 's', 104: 'h'}
>>> d = {x:ord(x) for x in "FishC"}
>>> d
{'F': 70, 'i': 105, 's': 115, 'h': 104, 'C': 67}
>>> ['ord() 函数用于返回 字符的 unicode 编码']
['ord() 函数用于返回 字符的 unicode 编码']
>>> d = {x:y for x in [1,3,5] for y in [2,4,6]}
>>> d
{1: 6, 3: 6, 5: 6}
>>> ['上述 依次返回‘1:2,1:4’,但是 1:4 会将 1:2 替换,因此最后返回 1:6,同理返回 3:6, 5:6, 这即是字典的不可重复性']
['上述 依次返回‘1:2,1:4’,但是 1:4 会将 1:2 替换,因此最后返回 1:6,同理返回 3:6, 5:6, 这即是字典的不可重复性']
>>>