程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python - Dictionary

編輯:Python

         A dictionary is equivalent to “ Key value pair ” Set , every last “ Key value pair ” All by “ key ” and “ value ” form .

“ key ” It's arbitrary immutable data , such as : Integers 、 Floating point numbers 、 character string 、 Tuples . however : list 、 Dictionaries 、 Assemble these mutable objects , Can not act as “ key ”. also “ key ” Do not repeat .

        “ value ” It can be arbitrary data , And it's repeatable .

Dictionary creation :

        1. In order to pass the {}、dict() To create a dictionary object :

>>> a={"name":"giaohu","age":18,"job":"student"}
>>> a=dict(name="giaohu",age=18,job="student")
>>> a
{'name': 'giaohu', 'age': 18, 'job': 'student'}

        2. adopt zip() Create a dictionary object :

Be careful :zip() Yes convert list to tuple , So it uses [ ].

>>> k = ['name','age','job']
>>> v = ['giaohu',18,'student']
>>> d = dict(zip(k,v))
>>> d
{'name': 'gaoqi', 'age': 18, 'job': 'student'}

        3.  adopt fromkeys Create a dictionary with an empty value :

>>> a = dict.fromkeys(['name','age','job'])
>>> a
{'name': None, 'age': None, 'job': None}

Access to dictionary elements :

        1. adopt [ key ] get “ value ”. If the key doesn't exist , False report .

>>> a = {'name':'giaohu','age':18,'job':'student'}
>>> a['name']
'giaohu'

        2. adopt get() Methods to get “ value ”. If the key doesn't exist , No return ; You can also set the return value when the specified key does not exist .

>>> a.get('name')
'giaohu'
>>> a.get('sex')
>>> a.get('sex',' One Man ')
' One Man '

        3. List all key value pairs

>>> a.items()
dict_items([('name', 'giaohu'), ('age', 18), ('job', 'student')])

        4.  List all the keys , List all the values

>>> a.keys()
dict_keys(['name', 'age', 'job'])
>>> a.values()
dict_values(['giaohu', 18, 'student'])

        5. len() The number of key-value pairs

>>> len(a)
3

        6. Test one “ key ” Is it in the dictionary

>>> 'name' in a
True

Dictionary elements add 、 modify 、 Delete :

        1. Add to dictionary “ Key value pair ”. If “ key ” Already exist , Then the old key value pair is overridden ; If “ key ” non-existent , Then add “ Key value pair ”.

>>> a = {'name':'giaohu','age':18,'job':'student'}
>>> a['adress']='bomb'
>>> a['job']='player'
>>> a
{'name': 'giaohu', 'age': 18, 'job': 'player', 'adress': 'bomb'}

        2.update() Update Dictionary

>>> a = {'name':'giaohu','age':18,'job':'student'}
>>> b={'name':'guichen','sex':'man'}
>>> a.update(b)
>>> a
{'name': 'guichen', 'age': 18, 'job': 'student', 'sex': 'man'}

        3.del() Delete key value pair

>>> a = {'name':'giaohu','age':18,'job':'student'}
>>> del(a['name'])
>>> a
{'age': 18, 'job': 'student'}

        4.pop() Delete the specified Key value pair , And return the corresponding “ The value object ”

>>> a.pop('age')
18
>>> a
{'job': 'student'}

        5.clear() Delete all key value pairs

>>> a = {'name':'giaohu','age':18,'job':'student'}
>>> a.clear()
>>> a
{}

       *6.popitem() : Randomly delete and return the key value pair .

Sequence unpacking :

         Sequence unpacking can be used for tuples 、 list 、 Dictionaries .

>>> x,y,z=(20,30,10)
>>> x
20
>>> y
30
>>> z
10
>>> [a,b,c]=[10,20,30]
>>> a
10
>>> b
20
>>> c
30

         When sequence unpacking is used for Dictionary , The default is right “ key ” To operate ; If you need to operate on key value pairs , You need to use items(); If necessary “ value ” To operate , You need to use values();

>>> s = {'name':'giaohu','age':18,'job':'student'}
>>> name,age,job=s # The default is to operate on the key
>>> name
'name'
>>> name,age,job=s.items() # Operate on key value pairs
>>> name
('name', 'giaohu')
>>> name,age,job=s.values() # Operate on values
>>> name
'giaohu'

Use dictionaries and lists to store table data :

a1 = {"name":"giao1","age":18,"salary":30000,"city":"bj"}
a2 = {"name":"giao2","age":19,"salary":20000,"city":"sh"}
a3 = {"name":"giao3","age":20,"salary":10000,"city":"sz"}
bl=[a1,a2,a3]
for i in range(len(bl)):
print(bl[i].get('name'),bl[i].get('age'),bl[i].get('salary'),bl[i].get('city'))


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved