Python basic data Type--dictionary

Source: Internet
Author: User

Dictionary

1. Variable sequence of "key-value" pairs,

2. ' Key ' can be any immutable sequence, such as Integer, real, plural, string, tuple, etc., do not allow repetition,' value ' can be repeated

3. You can use Globals () and locals () to view a dictionary of all global/local variables and values in the current scope

1 #Create a dictionary2>>keys = ['a','b','C']3>>values = [A]4>>adic =dict (Zip (keys,values))5>>Print(adict)6{'a': 1,'b': 2,'C': 3}7 8>>person = Dict (name='Jack', age=37)9>>Print(person)Ten{'name':'Jack',' Age': 37} One  A #Delete a dictionary or its elements ->>delperson[' Age'] ->>Print(person) the{'name':'Jack'} ->>person.pop ('name') - 'Jack' ->>Print(person) + {} -  + #use Clear () to empty the dictionary A>>person = Dict (name='Jack', age=37) at>>Print(person) -{'name':'Jack',' Age': 37} ->>person.clear () ->>Print(person) -{}

1 #Reading dictionary elements2>>adict = Dict (name='Nik', age=32,sex='Mans', work=False)3>>adict['name']4 'Nik'5>>adict.get (' Age')6327 8 #returns the key value pair9>>Adict.items ()TenDict_items ([('name','Nik'), (' Age', 32), ('Sex','Mans'), (' Work', False)]) One  A #return value ->>adict.values () -Dict_values (['Nik', 32,'Mans', False]) the  - #Return key ->>Adict.keys () -Dict_keys (['name',' Age','Sex',' Work'])
1 #Adding and modifying elements2>>perc = Dict (name='Nik', age=32,sex='Mans', work=False)3>>perc['Address'] ='WHT'4>>Print(ADITC)5{'Address':'WHT',' Age': 32,'name':'Nik','Sex':'Mans',' Work': False}6 7 #use Update to add all of the key value pairs of another dictionary to the current dictionary, adding from the header8>>perc.update ({'ID': 256,'Height': 175})9>>Print(PERC)Ten{'Address':'WHT',' Age': 32,'Height': 175,'ID': 256,'name':'Nik', One  'Sex':'Mans',' Work': False} A  - #generate 1000 random characters and count the occurrences of each character ->>Importstring the>>ImportRandom ->>x=string.ascii_letters+string.digits+string.punctuation ->>Print(x) - 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789! " #$%&\ ' () *+,-./:;<=>[email protected][\\]^_ ' {|} ~' +>>y=[random.choice (x) forIinchRange (1000)] ->>z ="'. Join (y) +>>d =dict () A>> forChinchZ: atD[ch] = D.get (ch,0) +1 ->>Print(d) - #more results, not listed -  - #Counter class statistic character count using collections module ->>ImportColllectionsImportCounter in>>freq =Counter (z) ->>Freq.items () to #the most frequently occurring +>>freq.most_common (1) -[('A', 18)] the #maximum number of times before 3 *>>freq.most_common (3) $[('A', 18), (')', 18), (':', 17)]

Python basic data Type--dictionary

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.