python-Dictionary (dict) processing

Source: Internet
Author: User
Tags shallow copy

A dictionary is the only type of mapping in the Python language.

A hash value (key, key) and a pointing object (value, value) in a mapped type object are a one-to-many relationship that is often considered a mutable hash table.

The object of the dictionary is mutable, which is a container type that can store any number of Python objects, which can also include other container types.

The data in the dictionary is unordered.

The keys and values in the dictionary are separated by ":", the items are separated from the items, the values in the dictionary must be unique, and the values can be not unique!

1. Create Dict

Simply put, a dictionary is a collection of key-value pairs enclosed in curly braces "{}".

General form:

>>> adict={' name ': ' Fly ', ' age ': +, ' sex ': ' Man '}
>>> bdict={}
>>> type (bdict)
<type ' Dict ' >
>>> type (adict)
<type ' Dict ' >

>>>

Use the Dict () function:

>>> cdict = Dict ()
>>> type (cdict)
<type ' Dict ' >
>>> cdict
{}
>>> ddict = Dict (([' A ', 1],[' B ', 2]))
>>> ddict
{' A ': 1, ' B ': 2}
>>> edict = dict (name= ' flyer ', sex= ' man ')
>>> Edict
{' name ': ' Flyer ', ' sex ': ' Man '}
>>>
2, dict the deletion and modification

>>> adict = {' age ': +, ' name ': ' Fly ', ' sex ': ' Man '}

Increase

Adict[new_key] = value

>>> adict[' telphone ']= 1888888888

>>> adict

{' Age ': +, ' telphone ': 1888888888, ' name ': ' Fly ', ' sex ': ' Man '}

By deleting

    • Del Adict[key] #删除指定key的项
    • Del adict #删除整个字典
    • Adict.pop (Key) #删除指定key的项
    • Adict.clear () #清空字典所有条目

>>> del adict[' age ']

>>> adict

{' Telphone ': 1888888888, ' name ': ' Fly ', ' sex ': ' Man '}

>>> adict

{' Telphone ': 1888888888, ' name ': ' Fly ', ' sex ': ' Man '}

>>> adict.pop (' Telphone ')

1888888888

>>> del adict #删除整个字典

If the deleted element does not exist

>>> adict = {' age ': +, ' name ': ' Fly ', ' sex ': ' Man '}

>>> del adict[' wife ']

Traceback (most recent):

File "<stdin>", line 1, in <module>

Keyerror: ' wife '

>>> Adict.pop (' wife ')

Traceback (most recent):

File "<stdin>", line 1, in <module>

Keyerror: ' wife '

>>>

Change

Adict[old_key] = New_value

>>> adict

{' Age ': +, ' name ': ' Fly ', ' sex ': ' Man '}

>>> adict[' name ']= ' FHR '

>>> adict

{' Age ': +, ' name ': ' FHR ', ' sex ': ' Man '}

>>>

Check

    • Adict.has_key (' key ') #有->true, no->false
    • In, not in #如: ' key ' in adict with->true, no->false
    • Adict.get (' key ') #获得key的值, there is a value that returns key, does not show
    • adict[' key '] #获得key的值, there is the value of key returned, no exception is reported

>>> adict

{' Age ': +, ' name ': ' FHR ', ' sex ': ' Man '}

>>> adict.has_key (' name ')

True

>>> Adict.has_key (' wife ')

False

>>> ' name ' in Adict

True

>>> ' wife ' in adict

False

>>>

>>> adict.get (' age ')
30
>>> adict.get (' wife ')
>>> print adict.get (' wife ')
None
>>> adict[' age ']
30
>>> adict[' wife ']
Traceback (most recent):
File "<stdin>", line 1, in <module>
Keyerror: ' wife '

>>>

3. Mapping type Operators

Standard type operator: +,-,*,/,<,>,<=,>=,==,!=,and,or,not

The dictionary does not support splicing and repeating operators (+,*)

Comparison of dictionaries

Compare the length of the dictionary to the number of dictionary elements, compare keys, and compare values

>>> adict = {' age ': +, ' name ': ' Fly ', ' sex ': ' Man ', ' Telphone ': 188888888}

>>> bdict = {' age ': +, ' name ': ' Fly ', ' sex ': ' Man '}

>>> cdict = {' age ': $, ' name ': ' Fly ', ' sex ': ' Man '}

>>> ddict = {' age ': +, ' name ': ' Fly '}

>>> edict = {' age ': +, ' name ': ' Fly '}

>>> CMP (ADICT,BDICT)

1

>>> CMP (BDICT,CDICT)

1

>>> CMP (DDICT,CDICT)

-1

>>> CMP (DDICT,EDICT)

0

>>>

4. Features of the dictionary

The value of the dictionary can be any Python object, key does not

1). The keys in the dictionary are unique, and if the same key is assigned two times, the latter value is remembered

>>> dict = {' name ': ' FJ ', ' name ': ' FHR '}

>>> Dict

{' name ': ' FHR '}

>>>

2). The key must be immutable and can be used as a number, as a string, or as a meta-ancestor, but not in the list.

>>> dict = {[' name ']: ' fjg ', ' Age ': 18}

Traceback (most recent):

File "<stdin>", line 1, in <module>

Typeerror:unhashable type: ' List '

5. Dictionary built-in functions & methods

    • Adict.clear (): Delete all elements in the dictionary

>>> adict = {' age ': +, ' name ': ' FHR ', ' sex ': ' Man '}

>>> Adict.clear ()

>>> adict

{}

    • Adict.copy (): Returns a copy of a shallow copy of a dictionary
    • Adict.fromkyes (S[,v]): Creates and returns a new dictionary with the elements in S as the key,v of the new dictionary, making the initial value of all keys in the new dictionary. V defaults to None
    • Adict.get (Key[,d]): Gets the value of key in the dictionary. If key does not exist, the default value of return d,d is None
    • Adict.has_key (Key): If there is a key in the query dictionary, it returns true, otherwise false
    • Adict.items (): Returns a list containing all (key, value) tuples
    • Adict.interitems (): Through Key:value iteration
    • Adict.interkeys (): Iterate through key
    • Adict.intervalues (): Iterate through value
    • Adict.keys (): Returns a list containing all keys for the dictionary
    • Adict.pop (K[,d]): If there is a key, delete and return the value of K, if key does not exist, and D is specified, then return D, if no d is specified, throws a Keyerror exception.
    • Adict.popitem (): randomly deletes any element and returns if Adict is empty, returns an exception
    • Adict.setdefault (key,default= "): Returns the value of key if key exists, or adict[key]=default to key if it does not exist
    • Adict.update ()
    • Adict.values (): Returns a list containing all the value of the dictionary
    • Adict.viewitems ()
    • Adict.viewkeys ()
    • Adict.viewvalues ()

python-Dictionary (dict) processing

Related Article

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.