Python's "Dictionary dict": creating, accessing, updating, deleting, viewing keys, values, key-value pairs; traversing; sorting

Source: Internet
Author: User
Tags delete key dota

A dictionary is another variable container model and can store any type of object.

Each key value (Key=>value) pair of the dictionary is separated by a colon (:), separated by commas (,), and the entire dictionary is enclosed in curly braces ({}) ;


The dictionary value can be used without restrictions on any Python object, either a standard object or a user-defined one, but the key is not available, and if the same key is assigned two times, the latter value will be remembered.

Values can take any data type, but the key must be immutable, such as a string, a number, or a tuple (a mutable type such as a list cannot be a key).



Create, Access:

Dict = {' Name ': ' Zara ', ' age ': 7, ' Class ': ' A ';
Dict2 = {  };//Empty dictionary
print "dict[' name ']:", dict[' name '];
Print "dict[' age ']:", dict[' age ';

The dictionary contains the list: dict={' yangrong ': ['], ' IT ', ' Xiaohei ': ['], ' DotA ']}

Dictionary contains dictionary: dict={' yangrong ': {"age": "The", "Job": "IT"}, "Xiaohei": {"' Age ': '", ' job ': ' DotA '}}


Modify, add:

dict[' age ' = 8; # Update existing entry
dict[' School ' = ' DPS School '; # ADD new entry
Delete:

Del dict[' Name ']; # The DELETE key is the ' Name ' entry
dict.clear ();     # Empty dictionary All entries
del dict;        # Delete Dictionary

To see if a specific key is included: Dict.has_key (k)
Returns False if the key returns true in the dictionary Dict
view all keys, values, (key, value) pairs: Dict.keys (), Dict.values (), Dict.items (), return value type as List

Traverse:

For d,x in Dict.items ():
print ' key: ' +d+ ', Value: ' +x


For k in Dict.keys ():
Print "Key:" +k+ ", Value:" +dict[k]

Sort:

# sorted by Key
Print sorted (Dict1.items (), Key=lambda d:d[0])

Or:

Sorted (D.items (), Lambda X, y:cmp (x[0), y[0)), or reverse order:
Sorted (D.items (), Lambda X, y:cmp (x[0), y[0), reverse=true) # sorted by value
Print sorted (Dict1.items (), Key=lambda d:d[1])

Or:

Sorted (D.items (), Lambda X, y:cmp (x[1), y[1)), or reverse order:
Sorted (D.items (), Lambda X, y:cmp (x[1), y[1), reverse=true)









List

Accessing elements by distance difference

Sequential access, slow speed

Small storage space Requirements


Dict

Accessing elements by index

Need large space, but visit soon


Set

Elements cannot be duplicated

Other and dict are basically similar

|, & 、-、 ^, respectively: And, pay, poor, deficit

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.