Python Learning (4) Data Structure-dict and pythondict

Source: Internet
Author: User

Python Learning (4) Data Structure-dict and pythondict
Dictionary type dict

A dictionary is composed of keys and values. A dictionary is also called an associated array or a hash table.

Dict assignment

Dict is enclosed in braces {}. Each key and value are separated by a colon (:), and each pair is separated by a comma. d = {'one': 1, 'two': 2, 'Three ': 3}

Keys must be unique, but values do not need to be. values can be of any data type, such as strings, numbers, or tuples. If the same key is assigned twice at creation, the latter value will be remembered;

The key must be unchangeable. Therefore, you can use numbers, strings, or tuples to act as keys.

Use dict () for forced conversion. The following format is acceptable. See the following example.

1 a = dict(one=1, two=2, three=3)2 b = {'one': 1, 'two': 2, 'three': 3}3 c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))4 d = dict([('two', 2), ('one', 1), ('three', 3)])5 e = dict({'three': 3, 'one': 1, 'two': 2})6 print(a == b == c == d == e)

 

Dict operations

The dictionary type is applicable to the values of corresponding keys. You must be familiar with dict access, modification, addition, deletion, and other operations.

Common usage is listed below. For details, refer:

Access value and modification Value

Dict_name ['key _ name'] can be accessed directly and can be changed directly.

1 d = {'name': 'zara ', 'age': 7, 'class': 'First'}; 2 print (d ['name'], d ['age'], d ['class']) # access key value to access dict_name ['key _ name'] 3 print ("% s's Age is % d, class is: % s. "% (d ['name'], d ['age'], d ['class']) # string output 4 # print (d ['name']) # If this key value does not exist, an error is returned. Note that the case is 5 d ['age'] = 86 print (d ['age']) # You can directly modify the key value. The value type is not limited.

 

Key addition and Deletion

Judge whether the key exists in dict and use key_name in/not in dict_name;

Add a key value, that is, assign dict_name ['new key_name '] = value to the new key; Delete the key value using del dict_name ['key _ name'];

Clear the dictionary using dict_name.clear ()

1 d = {'name': 'zara ', 'age': 7, 'class': 'First'}; 2 print (d ['name'], d ['age'], d ['class']) # access key value access dict ['key _ name'] 3 print ("% s's Age is % d, class is: % s. "% (d ['name'], d ['age'], d ['class']) # string output 4 # print (d ['name']) # If this key value does not exist, an error is returned. Note that the case is 5 d ['age'] = 8 6 print (d ['age']) # You can directly modify the key value, 7 print ('name' in d) # determine whether the key exists 8 print ('xxx' not in d) # determine whether the key does not exist 9 d ['sex'] = "Female" # Add a key value and assign a value directly to 10 print (d) 11 del d ['class'] # delete key value 12 print (d) 13 # del d ['class'] # If no key value exists, an error 14 d is returned. clear () # clear dict15 print (d)

 

Dict view objects

Len (dict_name) returns the number of key-value combinations in the dictionary. All keys dict_name.keys () can be extracted separately, and all values dict_name.values () can be obtained separately ()

1 d = {'name': 'zara ', 'age': 7, 'class': 'First'}; 2 print (len (d )) # Number of key-value combinations 3 print (d. keys (), type (d. keys () # It is of the dict_keys type and can be converted from list () to list or set () to set4 print (d. values () # It is of the dict_values type 5 print (d. items () # It is of the dict_items type.

 

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.