Python data type ----- dictionary, python data type -----

Source: Internet
Author: User

Python data type ----- dictionary, python data type -----

Today, we will summarize some operations on the python3.4 version dictionary.

 

A dictionary is an unordered Storage Structure in Python that stores key-value pairs. The keywords should be of an unchangeable type, such as strings, integers, and tuples containing immutable objects. Creating dictionaries is simple,
You can create a new dictionary in the form of d = {key1: value2, key2: value2}. You can also accept a key through dict, create a dictionary using a sequence or keyword parameter.
The key can be of multiple types, but the key is unique and unique. The value can be unique.

 

Dictionary: 

1. in statement to determine whether an element (key) is in a dictionary
2. The not statement indicates a negative effect on the following statements.
3. len can detect the number of dictionary elements
4. max returns the maximum element, while min returns the minimum element.
5. len (dict) returns the length of dict.
6. del dict [key]: Delete the key element in the dictionary dict. If the key does not exist, the KeyError type is incorrect.

 

 

Dictionary Method:  

1. d. clear () clear the dictionary d
2. d. copy (): perform a light copy on the dictionary d. A new dictionary with the same key-value pair as d is returned.
3. d. get (x [, y]) returns the value of key x in the dictionary d. If key x does not exist, return y. The default value of y is None.
4. d. items () returns all key-value pairs in dictionary d in the form of dict_items (in Python 2, d. iteritems () returns an iterator object for key-value pairs. Python 3 does not have the iteritems method)
5. d. pop (x [, default]) returns the value corresponding to the given key x and deletes the key-value pair from the dictionary. If x is not in the dictionary d, default is returned; if x is neither in d nor set by default, the KeyError type is incorrect.
6. d. popitem () returns and deletes random key-value pairs in the dictionary d.
7. d. setdefault (x, [, y]) returns the value corresponding to the key x in the dictionary d. If the key x does not exist, it returns y and returns x: y is added to the dictionary as a key-value pair. The default value of y is None.
8. d. update (x) adds all key-value pairs in dictionary x to dictionary d (repeated key-value pairs are replaced by key-value pairs in dictionary x)
9. d. keys () returns an iterator object for keys in the dictionary d in the form of dict_keys.
10. d. values () returns all the values in the dictionary in the form of dict_values to the iterator object for all values in the dictionary d.
11. d. fromkeys (iterable, value = None) returns a new dictionary. The key comes from iterable, and value is the key value.

 

>>> D = {'A': 1, 'B': 2}
>>> D
{'B': 2, 'A': 1}
>>> L = [('jonh', 18), ('nancy ', 19)]
>>> D = dict (L) # create from the list containing key values
>>> D
{'Jonh': 18, 'nancy ': 19}
>>> T = tuple (L)
>>> T
('Jonh', 18), ('nancy ', 19 ))
>>> D = dict (T) # create a table by using a tuples containing key values.
>>> D
{'Jonh': 18, 'nancy ': 19}
>>> D = dict (x = 1, y = 3) # create with keyword Parameters
>>> D
{'X': 1, 'y': 3}
>>> D [3] = 'Z'
>>> D
{3: 'Z', 'x': 1, 'y': 3}


>>> D
{3: 'Z', 'y': 3}
>>> L1 = [1, 2, 3]
>>> 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 call last ):
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]

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.