Python Data type-----dictionary

Source: Internet
Author: User
Tags shallow copy

Today to summarize some of the methods of operation of the python3.4 version dictionary.

  A dictionary is an unordered storage structure inside Python that stores key-value pairs of Key-value. Keywords should be immutable types, such as strings, integers, tuples containing immutable objects. The creation of dictionaries is simple,
You can create a new dictionary in the form of D = {key1:value2, key2:value2}, or you can create a dictionary by dict accepting a sequence pair or keyword argument that contains a key, Value.
Keys can be of multiple types, but keys are unique and not duplicates, values can be not unique

Dictionary: 

1, in statement, determine whether an element (key) is in a dictionary
2. The NOT statement indicates the negation of the following
3, Len can detect the number of elements in the dictionary
4, Max can return the largest element, min returns the smallest element
5. Len (dict) Returns the length of the Dict
6, del dict[key] Delete the element in the dictionary dict key, if key does not exist, it causes the Keyerror type error

Dictionary method:  

1. d.clear () empty dictionary D
2. d.copy () Shallow copy of Dictionary d, returns a new dictionary with the same key value pair as D
3, d.get (x [, y]) returns the value corresponding to the key x in the dictionary d, the key x does not exist, returns y, and the default value of Y is none .
4, d.items () returns all Key-value pairs in dictionary D as dict_items (python 2 d.iteritems () Returns an iterator object for a key-value pair, no Iteritems method in Python 3)
5, D.pop (x[, default])) returns the value corresponding to the given key x, and removes the key value pair from the dictionary, or returns default if X is not in dictionary d, which causes the Keyerror type error if x is not in D and the default is not set .
6, d.popitem () returns and deletes random key-value pairs in dictionary D
7, d.setdefault (x, [, y]) returns the value corresponding to key x in dictionary d, returns y if key x does not exist, and adds x:y as a key-value pair to the dictionary, and the default value for Y is None
8, d.update (x) Add dictionary x all key-value pairs to dictionary D (do not repeat, duplicate key-value pairs are substituted in dictionary d for key-value pairs in dictionary x)
9, D.keys () Returns an iterator object for the key in Dict_keys form for all keys in dictionary D
10, d.values () return all values in the dictionary as dict_values to the iterator object for all values in dictionary D
11, D.fromkeys (iterable, value=none) Returns a new dictionary with the key from Iterable,value as the key value

>>> d = {' a ': 1, ' b ': 2}
>>> D
{' b ': 2, ' a ': 1}
>>> L = [(' jonh '), (' Nancy ', +)]
>>> d = dict (L) #通过包含键值的列表创建
>>> D
{' jonh ': ' Nancy ': +}
>>> T = tuple (L)
>>> T
(' jonh ', ('), (' Nancy ' , +)
>>> d = dict (T) #通过包含键值的元组创建
>>> D
{' jonh ': ' Nancy ': +}
>>> d = dict (x = 1, y = 3) #通过关键字参数创建
>>> D
{' x ': 1, ' y ': 3}
>>> d[3] = ' Z '
>>> D
{3: ' z ', ' x ': 1, ' y ': 3}


>>> D
{3: ' z ', ' y ': 3}
>>> L1 = [+]
>>> D.fromkeys (L1)
{1:none, 2:none, 3:none}
>>> {}.fromkeys (L1, ' Nothing ')
{1: ' Nothing ', 2: ' nothing ', 3: ' Nothing '}
>>> Dict.fromkeys (L1, ' over ')
{1: ' over ', 2: ' over ', 3: ' over '}


>>> D
{3: ' z ', ' x ': 1, ' y ': 3}
>>> d[3]
' Z '
>>> d[' x ']
1
>>> d[0]
Traceback (most recent):
File "<pyshell#26>", line 1, in <module>
d[0]
keyerror:0


>>> d = {' z ': 5, ' x ': 1.5, ' y ': 3}
>>> d.items ()
dict_items ([' z ', 5), (' x ', 1.5), (' y ', 3)])
>>> list (d.items ())
[(' z ', 5), (' x ', 1.5), (' y ', 3)]

>>> D.keys ()
Dict_keys ([' z ', ' x ', ' y '])
>>> for x in D.keys ():
Print (x)
Z
x
y

>>> d1 = {' x ': 1, ' y ': 3}
>>> d2 = {' x ': 2, ' z ': 1.4}
>>> d1.update (d2)
>>> D1
{' z ': 1.4, ' x ': 2, ' y ': 3}

>>> D1
{' z ': 1.4, ' x ': 2, ' y ': 3}
>>> d1.values ()
dict_values ([1.4, 2, 3])
>>> list (d1.values ())
[1.4, 2, 3]

Python Data type-----dictionary

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.