"Python3" dictionary

Source: Internet
Author: User

The dictionary is the Key-value data type

Create
info = {    ' stu01 ': ' Jack ',    ' stu02 ': ' Mike ',    ' stu03 ': ' Jay '}

Features of the dictionary:

    • Dict is disordered.
    • Key must be unique and inherently heavy

Increase

>>>info[' stu05 ' = ' kit ' #自动创建 {' stu01 ': ' Jack ', ' stu02 ': ' Mike ', ' stu03 ': ' Jay ', ' stu05 ', ' kit '}

By deleting

>>> Info.pop ("stu03") #标准删除, you must add the parameter {' stu01 ': ' Jack ', ' stu02 ': ' Mike '}
>>> del info[' stu03 ' #通用删除 {' stu01 ': ' Jack ', ' stu02 ': ' Mike '}
>>> Info.popitem () #随机删除 {' stu01 ': ' Jack ', ' stu03 ': ' Jay '}

Change

>>>info[' stu02 ' = ' bob '
{' stu01 ': ' Jack ', ' stu02 ': ' Bob ', ' stu03 ': ' Jay '}

Check

>>> ' stu02 ' in info #标准用法True >>> info.get (' stu02 ')  #获取 ' Mike ' >>> info["stu1105"]  # If a key does not exist, an error, get will not, does not exist only return Nonetraceback (most recent call last):  File "<stdin>", line 1, in <module> Keyerror: ' stu1105 '

Multi-level dictionary nesting and manipulation
City = {'    Asia ': {'        China ': [' Beijing ', ' Shanghai ', ' Guangzhou '],        ' Russia ': [' Moscow ', ' St Petersburg '],        ' Japan ': [' Tokyo ', ' Nagoya ', ' Osaka ']    },    ' Europe ': {        ' uk ': [' London ', ' Birmingham ']    },    ' Oceania ': {        ' Australia ': [' Sydney ', ' Canberra ']    }
city[Asia] [China][2]= ' Shenzhen ' #将广州改成深圳
Other
 #values 
>>> info.values ()
[' Jack ', ' Mike ', ' Jay ']

#keys
>>> Info.keys ()
[' stu01 ', ' stu02 ', ' stu03 ']

#setdefault #键值存在则罢, non-existent add
>>> info.setdefault (' stu06 ' Jane ')
{' stu01 ': ' Jack ', ' stu02 ': ' Mike ', ' stu03 ': ' Jay ', ' stu06 ', ' Jane '}
>>> Info.setdefault (' Stu02 ', ' Jane ')
{' stu01 ': ' Jack ', ' stu02 ': ' Mike ', ' stu03 ': ' Jay '}

#update #合并字典, overwrite
>>> b if key value repeats = {1:2,3:4, ' stu01 ': ' Kit '}
>>> info.update (b)
{' stu01 ': ' Kit ', ' stu02 ': ' Mike ', ' stu03 ': ' Jay ', 1:2,3:4}

#items #字典转列表 with Ganso
>>>info.items ()
[(' Stu01 ': ' Jack '), (' stu02 ': ' Mike '), (' stu03 ': ' Jay ')]

#通过一个列表生成默认dict
>>> Dict.fromkeys ([2], ' TESTD ')
{1: ' testd ',: ' Testd ', 3: ' TESTD '}
>>>c=dict.fromkeys ([1,2,3],[' Jack ', ' Sandra '])
>>>c[2][1]= ' Happy ' #全改了
{1:[' Jack ', ' happy '],3:[' Jack ', ' Happy '],3:[' Jack ', ' Happy ']}

Cycle
#方法1for key in Info:  #高效    print (Key,info[key]) #方法2for key,value in    info.items (): #先把dict转成list, MO When the data is large Print (Key,value)

"Python3" dictionary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.