Python automated Operations 1-dictionaries

Source: Internet
Author: User
Tags shallow copy

Dictionary factory functions:

x = {' A ': +, ' B ': $, ' C ': 300}


Dictionary index: #显示没有顺序, unlike lists and functions


Print x[' a ']

100


Dictionary method:


Delete:

X.pop (' a ') #需给出索引参数

Print X


X.popitem () #不需要索引参数, randomly deleted


Delete all elements:


X.clear ()


Print X

Output: {}



Items and Iteritems output dictionary key-value


X.items () X.iteritems ()


Keys and Iterkeys output dictionary middle key


X.keys () X.iterkeys ()


Values and itervalues in the output dictionary


X.values () x.itervalues ()


Get Get Dictionary Key-value (more moderate output)


X.get (' a ')

Output: 100

X.get (' d ')

No output


Has_key confirm if the key is in the dictionary


X.has_key (' a ')

Output: True

X.has_key (' d ')

Output: False


Fromkeys the given key to create a new dictionary


A = Dict.fromkeys ([' Name ', ' Age '], (100))


SetDefault access to the dictionary key-value, if the key is not incremented, the value defaults to none


X.setdefault (' e ', 100)


Update list, add a list of key-values to another list key-value, repeat remove


x = {' A ': +, ' B ': $, ' C ': 300}


z = {' d ': $, ' E ': 6000}


X.update (z)


Print X

Output: {' A ': +, ' C ': +, ' B ': $, ' e ': 6000, ' d ': 500}



A shallow copy of a dictionary: if the key-a value, does not change the value of the original dictionary, if the key-multi-value (list), there will be a change at the same time


z = x.copy ()


Print Z

Output: {' A ': +, ' B ': $, ' C ': 300}


Z[' C '] = 400


Print Z

Output: {' A ': +, ' C ': +, ' B ': 200}


Print X

Output: {' A ': +, ' C ': +, ' B ': 200}


x = {' A ': +, ' C ': +, ' B ': [600, 200]}


y = x.copy ()


Print Y

Output: {' A ': +, ' C ': +, ' B ': [600, 200]}


y[' B '][0] = 300


Print Y

Output: {' A ': +, ' C ': +, ' B ': [300, 200]}


Print X

Output: {' A ': +, ' C ': +, ' B ': [300, 200]}

A deep copy is needed at this time.


From copy import deepcopy


y = x.deepcopy ()


Python automated Operations 1-dictionaries

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.