Python Learning note Dictionary (iv)

Source: Internet
Author: User

A dictionary is the only type of mapping in Python that stores data in the form of key-value pairs (key-value). Python computes the hash function of key and determines the storage address of value based on the computed result, so the dictionary is stored out of order and the key must be hashed. A hash indicates that a key must be an immutable type, such as a number, a string, a tuple.

The Dictionary (dictionary) is the most flexible built-in data structure type in addition to the list of unexpected python. A list is an ordered combination of objects, and a dictionary is a collection of unordered objects. The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.

To create a dictionary:

Shop = {' iphone ': +, ' book ': ' python '}shop2 = Dict ((' iphone7s ', ' new ') ') print (shop) print (SHOP2)

Output:

{' iphone ': $, ' book ': ' Python '}

{' iphone7s ': ' New '}


Corresponding operation:

1, increase

Shop = {}shop[' iphone7s "] = ' new ' shop[' price '] = 8000print (shop) Shop1 = Shop.setdefault (' price ', 9000) #键存在, no change, Returns the value corresponding to the corresponding key in the dictionary print (SHOP1) shop2 = Shop.setdefault (' buy ', ' JD ') #键不存在, adds a new key-value pair in the dictionary, and returns the corresponding value print (SHOP2) print (shop)

Output:

{' iphone7s ': ' New ', ' Price ': 8000}

8000

Jd

{' iphone7s ': ' New ', ' price ': 8000, ' buy ': ' JD '}


2. Check

Shop = {' iphone7s ': ' New ', ' price ': 8000, ' buy ': ' JD '}print (Shop.items ()) print (Shop.keys ()) print (Shop.values ()) Print ( shop[' buy '] print (shop.get (' buy ', false)) print (' Buy ' in shop) print (("Shop.get (' buys ', false)" Print (List (Shop.values ( )))

Output:

Dict_items ([' iphone7s ', ' new '), (' Price ', 8000), (' Buy ', ' JD ')])

Dict_keys ([' iphone7s ', ' price ', ' buy '])

Dict_values ([' New ', 8000, ' JD '])

Jd

Jd

False

True

[' New ', 8000, ' JD ']


3, change

Shop = {' iphone7s ': ' New ', ' price ': 8000, ' buy ': ' JD '}shop[' iphone7s '] = ' old ' Shop1 = {' iphone5 ': ' True ', ' Size ': 500}shop.u Pdate (SHOP1) print (shop)

Output:

{' iphone7s ': ' Old ', ' price ': 8000, ' buy ': ' JD ', ' iphone5 ': ' True ', ' Size ': 500}


4. By deleting

Shop = {' iphone7s ': ' Old ', ' price ': 8000, ' buy ': ' JD ', ' iphone5 ': ' True ', ' size ': 500}del shop[' size ' #删除字典中指定键值对print (sho p) Shop1 = Shop.pop (' iphone5 ') #删除字典中指定键值对 and returns the value of the key value pair print (SHOP1) print (shop) Shop2 = Shop.popitem () #随机删除某组键值对, and returns the value in tuples print (SHOP2) print (shop) shop.clear () # Empty the dictionary print (shop)

Output:

{' iphone7s ': ' Old ', ' price ': 8000, ' buy ': ' JD ', ' iphone5 ': ' True '}

True

{' iphone7s ': ' Old ', ' price ': 8000, ' buy ': ' JD '}

(' Buy ', ' JD ')

{' iphone7s ': ' Old ', ' Price ': 8000}

{}


5. Built-in method

Dict.fromkeys

Dic6=dict.fromkeys ([' host1 ', ' host2 ', ' host3 '], ' test ') print (dic6) dic6[' host2 ']= ' abc ' Print (DIC6) dic6= Dict.fromkeys ([' host1 ', ' host2 ', ' host3 '],[' test1 ', ' tets2 ']) print (dic6) dic6[' host2 '][1]= ' test3 ' Print (DIC6)

Output:

{' host1 ': ' Test ', ' host2 ': ' Test ', ' host3 ': ' Test '}

{' host1 ': ' Test ', ' host2 ': ' abc ', ' Host3 ': ' Test '}

{' host1 ': [' test1 ', ' tets2 '], ' host2 ': [' test1 ', ' tets2 '], ' host3 ': [' test1 ', ' Tets2 ']}

{' host1 ': [' test1 ', ' test3 '], ' host2 ': [' test1 ', ' test3 '], ' host3 ': [' test1 ', ' Test3 ']}


Dic={5: ' 555 ', 2: ' 666 ', 4: ' 444 '}print (5 in DIC) print (sorted (Dic.items ()))

Output:

True

[(2, ' 666 '), (4, ' 444 '), (5, ' 555 ')]


dic5={' name ': ' Joker ', ' age ': 18}for i in Dic5:print (I,dic5[i])-I,v in Dic5.items (): Print (I,V) for item in DIC5. Items (): Print (item)

Output:

Name Joker

Age 18

Name Joker

Age 18

(' name ', ' Joker ')

(' age ', 18)


This article is from the "on_the_road" blog, make sure to keep this source http://cqtesting.blog.51cto.com/8685091/1958823

Python Learning note Dictionary (iv)

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.