Python Array Dictionary dict notes

Source: Internet
Author: User
The code is as follows Copy Code

#!/usr/bin/env python
#-*-Encoding:utf-8-*-
Dict2 = {}
A = ' a '
Dict2.setdefault (a,1) #加入一个键和值 a,1
A = ' B '
Dict2.setdefault (a,1) #加入一个键和值 b,1
A = ' a '
Dict2.setdefault (a,2) #加入一个键和值 a,1, the resulting duplicate key cannot be repeated by adding {' A ': 1, ' B ': 1}

Print Dict2


#遍历key和value
For key in Dict2.keys ():
print ' key ', key, ' value ', Dict2[key]

Print Dict2.has_key (' name ') #判断是否含有一个key

More methods

Method Name Operation
dict. Clear () Delete all elements in a dictionary
dict. Copy () Returns a copy of a dictionary (shallow copy)
DICT.FROMKEYSC (Seq,val=none) Creates and returns a new dictionary, which is the key of the dictionary in Seq, and Val does the initial value for all keys in the dictionary (none is default if this value is not provided)
Dict.get (Key,default=none) Returns the value of the key in the dictionary dict, and returns the value of default if this key is not present in the dictionary (note that the default value for parameter default is None)
Dict.has_key (Key) Returns true if the key is present in the dictionary, or false. After the Python2.2 version is introduced in and not, this method is almost obsolete, but still provides a working interface.
Dict.items () Returns a list containing the dictionary (key, value) to tuple
Dict.keys () Returns a list containing the keys in the dictionary
Dict.values () Returns a list containing all the values in the dictionary
Dict.iter () Methods Iteritems (), Iterkeys (), itervalues () are the same as their corresponding non iterative methods, but they return an iteration rather than a list.
Dict.pop (key[, default]) Similar to the method get (), if the key key in the dictionary exists, deletes and returns Dict[key], and throws a Keyerror exception if the key does not exist and the value of default is not given.
Dict.setdefault (Key,default=none) Similar to the method set (), if the key is not present in the dictionary, it is assigned a value by Dict[key]=default.
Dict.setdefault (Key,default=none) Similar to the method set (), if the key is not present in the dictionary, it is assigned a value by Dict[key]=default.

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.