Python3 Dictionary Operations

Source: Internet
Author: User

字典:创建方式: 1、正常创建 a = {‘name‘: ‘azj‘, ‘age‘: 23, ‘mail‘: ‘[email protected]‘} 2、通过工厂函数dict创建 a = dict(([‘name‘, ‘azj‘], [‘age‘, 23], [‘email‘, ‘[email protected]‘])) 3、 a = {}.fromkeys((‘azj‘, ‘tom‘, ‘lili‘), 11) {‘azj‘: 11, ‘tom‘: 11, ‘lili‘: 11}>>> a = {‘name‘: ‘azj‘, ‘age‘: 23}>>> a{‘name‘: ‘azj‘, ‘age‘: 23}>>> ‘%(name)s:%(age)s‘ % a‘azj:23‘

Modify:
adict = {' name ': ' Azj ', ' Age ': 26}

>> adict
{' name ': ' Azj ', ' Age ': 26}
>> adict[' age '] = 22
>> adict
{' name ': ' Azj ', ' age ': 22}
>> adict[' phone '] = ' 15901037927 ' #字典里有这个字段修改, no then add this field
>> adict
{' name ': ' Azj ', ' age ': ' Phone ': ' 15901037927 '}

Delete:
Adict
{' name ': ' Azj ', ' age ': ' Phone ': ' 15901037927 '}

>> adict.pop (' name ') #pop默认弹出并删除最后一项, the dictionary should be unordered in the dictionary so you need to specify the field information that pops up
' Azj '
>> adict
{' Age ': $, ' phone ': ' 15901037927 '}
Adict.popitem () # # #随机删除并弹出一项内容
(' phone ', ' 15901037927 ')
>> adict
{' Age ': 22}
>> adict.clear () #清空列表
>> adict
{}
>> del adict #删除列表
>> adict

len(字典)    #统计字典内的元素数目>>> len(adict)2

Copy of Dictionary:

>> a = {' name ': ' Azj ', ' age ': 23}
>> A
{' name ': ' Azj ', ' age ': 23}
>> B = A #内容赋值, pointing to the same memory space
>> b
{' name ': ' Azj ', ' age ': 23}
>> A
{' name ': ' Azj ', ' age ': 23}
>> ID (a)
140165656248536
>> ID (b)
140165656248536
>> C = a.copy ()
>> C
{' name ': ' Azj ', ' age ': 23}
>> ID (c)
140165656248752

Xxx.get (' key ', []) #列出字典的value, if no key is returned by default, none, custom return information []

>> Dict
{' name ': ' Azj ', ' Age ': 26}
>> dict.pop (' age ')
26
>> Dict
{' name ': ' AZJ '}
>> dict.get (' name ')
' Azj '
>> dict.get (' age ')
>> dict.get (' age ', ' not found ')
' Not found '

Xxx.setdefault () #当字典里面没有该值的情况下进行添加, some words cannot be added

>> Dict
{' name ': ' AZJ '}
>> dict.setdefault (' age ', 26)
26
>> Dict
{' name ': ' Azj ', ' Age ': 26}
>> dict.setdefault (' age ', 25)
26
>> Dict
{' name ': ' Azj ', ' Age ': 26}

>> Dict.keys () #列出当前字典所有的key
Dict_keys ([' Name ', ' age '])
>> dict.values () #列出当前字典的所有value
Dict_values ([' AZJ ', 26])

>> Dict.items () #列出当前字典的key和value
Dict_items ([' Name ', ' Azj '), (' Age ', 26)])

# #key, Value rollover

>> {Value:key for key, value in Dict.items ()}
{' AZJ ': ' Name ',: ' Age '}
#互换的时候注意, the same value can cause element loss
>> a = {}.fromkeys (' Tom ', ' Bob ', ' Lili '), 18)
>> A
{' Tom ': ' Bob ': Lili ': 18}
>> {Val:key for key, Val in A.items ()}
{: ' Lili '}

Stitching of dictionaries:

>> a = {' name ': ' AZJ '}
>> A
{' name ': ' AZJ '}
>> B = {' Age ': 26}
>> A.update (b)
>> A
{' name ': ' Azj ', ' Age ': 26}

Python3 Dictionary Operations

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.