Python Day5 Dictionary

Source: Internet
Author: User

Python dictionary
# Data Type partitioning: mutable data type, immutable data type # immutable data type: ganso bool str int HASH # mutable data type: List Dict (dictionary) set (set) non-hash # Dict key (must be hash) value (Arbitrary data class # dict Advantages: Fast query speed (binary search) # Store A lot of relational data # Features: unordered (before 3.5 version) Dic1 = {"Age": "" Name ":" Break "," sex ":" male "}# increase D ic1["hobby"] = "girly" # does not add # print (DIC1) # {' Age ': ' ' name ': ' Break ', ' hobby ': ' Giry ', ' sex ': ' Male '}dic1.setdef Ault ("Game") # does not increase, there is an overlay (the default value added is None) # print (dic1) # {' Sex ': ' Male ', ' age ': ', ' ' name ': ' Break ', ' game ': none, ' Hobb Y ': ' girly '}# delete dic1.pop ("game") # Pass in the key to be deleted (The return value is the value of the corresponding key) # Print (Dic1.pop ("Break666", "no corresponding key (None)") # If you need to delete the K EY will return a second parameter < no corresponding key (none) ># print (dic1) # {' Hobby ': ' girly ', ' age ': ' Sex ': ' Male ', ' name ': ' Break '}# p  Rint (Dic1.popitem ()) # randomly delete the 3.6 version after the default delete the last one returns a tuple (' name ', ' Break ') # print (dic1) # {' Hobby ': ' girly ', ' sex ': ' Male ', ' Age ': 18}del dic1["Hobby"] # Delete the corresponding key if key does not have an error, it is recommended to use pop# print (DIC1) # {' Sex ': ' Male ', ' name ': ' Break ', ' age ': 18}# Del dic1 # Direct Delete Dictionary # PRint (DIC1) # error nameerror:name ' Dic1 ' is not defined# dic1.clear () # Empty Dictionary # Print (dic1) # {}# change dic1[' age '] = 19 # Direct Assignment Print (DIC1) # updatediv = {"Name": "Box", "width": "200px", "height": "200px", "Background-color": "Red"}dic1.update (div) # Update Div to dic1 inside only change dic1,div do not change # print (dic1) # {' Sex ': ' Male ', ' height ': ' 200px ', ' width ': ' 200 Px ', ' age ': +, ' name ': ' Box ', ' background-color ': ' Red '}# print (div) # {' Name ': ' box ', ' Height ': ' 200px ', ' width ': ' 200p X ', ' background-color ': ' Red '}# as List with # print (Dic1.keys (), type (# Dic1.keys ())) # Type is the dictionary key type (dict_keys) Dict_keys (['     Background-color ', ' age ', ' width ', ' name ', ' sex ', ' height ']) # <class ' Dict_keys ' ># print (Dic1.values (), type (# Dic1.values ()) # type is the dictionary values type (dict_values) dict_values ([' Red ', ' 200px ', ' box ', ' Male ', ' 200px ')] <class# ' Dict_values ' ># print (Dic1.items (), type (# Dic1.items ())) # type is the dictionary items type (dict_items) dict_items ([' Background-color ', ' Red '), (' age ', +), (' WidtH ', ' 200px '), (' Name ', # ' box '), (' Sex ', ' Male '), (' Height ', ' 200px ')]) <class ' Dict_items ' >a, b, C = [1, 2, 3]print ( A, B, c) ' For I in Dic1:print (i) # All is key print (Dic1[i]) # All is value ' # for K, V in Dic1.items (): # Prin T (k, v) # Get Get key value # print (dic1["name1"]) # error # Print (Dic1.get ("name1", "no This Key")) # no default return of none can change the default return value ' info = Input (">>>") for I in Info:if I.isalpha (): info = info.replace (i, "") Print (Len (info.sp Lit ())) "

  

Python Day5 Dictionary

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.