py-字典

Python 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> dict={}
>>> type(dict)
<class 'dict'>
>>> dict = dict()
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    dict = dict()
TypeError: 'dict' object is not callable
>>> d = dict()
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    d = dict()
TypeError: 'dict' object is not callable
>>> dict
{}
>>> #字典操作---增刪改查
>>> #key--value
>>> dict{ 'liu':12,'yong':123}
SyntaxError: invalid syntax
>>> dict['first']=123
>>> dict
{'first': 123}
>>> dict['first']=123
>>> dict
{'first': 123}
>>> dict[12]='py'
>>> dict
{'first': 123, 12: 'py'}
>>> dict[12]
'py'
>>> dict.keys()
dict_keys(['first', 12])
>>> dict.values()
dict_values([123, 'py'])
>>> dict.items()
dict_items([('first', 123), (12, 'py')])
>>> list_value=[1,2,3,4]
>>> dict['first']=list_value
>>> dict
{'first': [1, 2, 3, 4], 12: 'py'}
>>> dict.pop()
Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    dict.pop()
TypeError: pop expected at least 1 arguments, got 0
>>> dict[12].pop()
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    dict[12].pop()
AttributeError: 'str' object has no attribute 'pop'
>>> #字典裏面可以套用字典
>>> dict={('first',1),('seconf',2)}
>>> dict
{('first', 1), ('seconf', 2)}
>>> dict['first']+=10
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    dict['first']+=10
TypeError: 'set' object is not subscriptable
>>> dict['first']
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    dict['first']
TypeError: 'set' object is not subscriptable
>>> dict['first']=10
Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    dict['first']=10
TypeError: 'set' object does not support item assignment
>>> dict
{('first', 1), ('seconf', 2)}
>>> dict.pop()
('first', 1)
>>> dict
{('seconf', 2)}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章