Python Learning (iv) data structure--dict

Source: Internet
Author: User

Dictionary type Dict

A dictionary consists of a key (key) and a corresponding value (value). Dictionaries are also referred to as associative arrays or hash tables.

Dict Assignment Value

Dict the whole in curly braces {}, each key and value separated by a colon (:), each pair separated by commas; d = {'one': 1, 'one ' : 2, 'three': 3}

The key must be unique, but the value is not necessary; The value is preferable to any data type, such as String, number, or tuple; If the same key is assigned two times at the time of creation, the latter value is remembered;

Keys must be immutable, so you can use a number, a string, or a tuple to act as a list.

Use the Dict () cast to accept the following form, see the example below

1A = Dict (one=1, two=2, three=3)2b = {' One': 1,' Both': 2,'three': 3}3c = dict (Zip ([' One',' Both','three'], [1, 2, 3]))4D = Dict ([(' Both', 2), (' One', 1), ('three', 3)])5E = Dict ({'three': 3,' One': 1,' Both': 2})6 Print(A = = b = = C = = d = e)

Dict operation

The dictionary type is applicable to the value of the corresponding key, and it is very practical for dict to access, modify, add, delete and so on.

The following is a list of common uses, please refer to:

Accessing values, modifying values

dict_name[' Key_name ') direct access to the value and can be changed directly

1D = {'Name':'Zara',' Age': 7,'Class':' First'};2 Print(d['Name'],d[' Age'],d['Class'])#access key value Access dict_name[' Key_name ']3 Print("%s ' s is%d, class is:%s."% (d['Name'], d[' Age'],d['Class']))#string Output4 #print (d[' name ']) # No such key value, will error, note case5d[' Age'] = 86 Print(d[' Age'])#can directly modify the value of the key, the number type unlimited

Key additions, deletions

Determine if key exists in Dict, use Key_name in/not in Dict_name;

Add the key value to the new key assignment dict_name[' new key_name ' = value, delete the key value using del dict_name[' Key_name '];

Empty dictionary using Dict_name.clear ()

1D = {'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 is%d, class is:%s."% (d['Name'], d[' Age'],d['Class']))#string Output4 #print (d[' name ']) # No such key value, will error, note case5d[' Age'] = 86 Print(d[' Age'])#can directly modify the value of the key, the number type unlimited7 Print('Name' inchD#determine if a key exists8 Print('XXX'  not inchD#determine if the key does not exist9d['Sex'] ="Female"               #Increase the value of the key directly assigned toTen Print(d) One deld['Class']#Delete a key value A Print(d) - #del d[' Class '] # If no key value, will error -D.clear ()#Empty Dict the Print(d)

Dict View Objects

Len (dict_name) returns the number of dictionary key-value combinations, which can be taken out of all keys Dict_name.keys (), and all the values are taken out individually dict_name.values ()

1D = {'Name':'Zara',' Age': 7,'Class':' First'};2 Print(Len (d))#number of combinations of key values3 Print(D.keys (), type (D.keys ()))#is a dict_keys type, the list () can be converted to list or set () to set4 Print(D.values ())#for the dict_values type5 Print(D.items ())#for the Dict_items type

Python Learning (iv) data structure--dict

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.