Class notes: Python Basics-dictionaries

Source: Internet
Author: User
Tags string methods

Two main features of the Python Dictionary: unordered, key unique

#字典创建
dic={'name':'Alex'}#The first form ofDic2=dict (('name','Alex'),))#The second form ofPrint(dic2) dic3=dict ([['name','Alex'],])#The third form ofPrint(DIC3)#dic1={' name ': ' Alex '}#dic1[' age ']=18#print (DIC1)#key exists, does not change, returns the value corresponding to the corresponding key in the dictionary#ret=dic1.setdefault (' age ',)#print (ret)###键不存在, add a new key-value pair in the dictionary and return the corresponding value#ret2=dic1.setdefault (' Hobby ', ' girl ')#print (DIC1)#print (Ret2)#Search through the keys to find#dic3={' age ': ' Name ': ' Alex ', ' hobby ': ' Girl '}##print (dic3[' name '])##Print (List (Dic3.keys ())) #输出所有键#Print (List (dic3.values ())) #输出所有键值#Print (List (Dic3.items ())) #输出所有键值#Dictionary Modification#li=[1,2,34,4]#li[2]=5#dic3={' age ': ' Name ': ' Alex ', ' hobby ': ' Girl '}#dic3[' age ']=55#print (DIC3)#dic4={' age ': ' Name ': ' Alex ', ' hobby ': ' Girl '}## dic5={' 1 ': ' 111 ', ' 2 ': ' 222 '}#dic5={' 1 ': ' 111 ', ' name ': ' 222 '}##dic4.update (DIC5)#print (DIC4)#print (DIC5)#dic5 = {' name ': ' Alex ', ' age ': +, ' class ': 1}#dic5.clear () # Empty the dictionary#print (DIC5)#del dic5[' name '] #删除字典中指定键值对#print (DIC5)#Print (Dic5.pop (' age ')) #删除字典中指定键值对 and returns the value of the key-value pair#ret=dic5.pop (' age ')#print (ret)#print (DIC5)#a = Dic5.popitem () #随机删除某组键值对, and returns the value in tuple form#print (A, dic5)#del dic5 #删除整个字典#print (DIC5)#5 Other operations and the methods involved#Dic6=dict.fromkeys ([' host1 ', ' host2 ', ' host3 '], ' test ')#print (dic6) #{' host3 ': ' Test ', ' host1 ': ' Test ', ' host2 ': ' Test '}##dic6[' host2 ']= ' abc '#print (DIC6)#Dic6=dict.fromkeys ([' host1 ', ' host2 ', ' host3 '],[' test1 ', ' tets2 '])#print (dic6) #{' host2 ': [' test1 ', ' tets2 '], ' host3 ': [' test1 ', ' tets2 '], ' host1 ': [' test1 ', ' tets2 ']}##dic6[' host2 '][1]= ' test3 '#print (dic6) #{' host3 ': [' test1 ', ' test3 '], ' host2 ': [' test1 ', ' test3 '], ' host1 ': [' test1 ', ' Test3 ']}#Av_catalog = {#"Europe and America": {#"www.youporn.com": ["a lot of free, the world's largest", "General quality"],#"www.pornhub.com": ["a lot of free, also very big", "quality than Yourporn High"],#"letmedothistoyou.com": ["mostly selfie, high-quality pictures many", "resources are not many, update slow"],#"x-art.com": ["high quality, really high", "all charges, Dick than please bypass"]# },#"Japan and Korea": {#"Tokyo-hot": ["How the quality is not clear, the individual has not liked the Japanese and Korean fan", "Heard is charged"]# },#"Continent": {#"1024": ["All free, really good, good Life Peace", "server in foreign countries, slow"]# }# }#av_catalog[' Europe and America '] ["www.youporn.com"][1]= ' high-definition afternoon horse 'DiC={5:'555', 2:'666', 4:'444'}#Dic.has_keys (5)#print (5 in dic)#print (sorted (Dic.items ()))#dic5={' name ': ' Alex ', ' age ':#For i in DIC5:#print (I,dic5[i]) +#For i,v in Dic5.items ():#print (i,v)#String Manipulation#a= "Let ' s Go"#print (a)#1 * Repeating output string#print (' Hello ' *20)#2 [], [:] Get the character in the string by index, here and the list of slice operation is the same, the concrete content see list#print (' HelloWorld ' [2:])#keyword in#print (123 in [23,45,123])#print (' e2l ' in ' Hello ')#4% format string#print (' Alex is a good teacher ')#print ('%s is a good teacher '% ' Alex ')#5#a= ' 123 '#b= ' abc '#d= ' "## # c=a+b## # Print (c)# ##c= '. Join ([a,b,d])#print (c)#built-in methods for strings#st= ' Hello Kitty {name} is {age} '##Print (St.count (' L ')) # Number of statistics elements#print (St.capitalize ()) # Capitalize first letter#Print (St.center (50, ' # ')) # Center#Print (St.endswith (' Tty3 ')) # to determine whether to end with a certain content#Print (St.startswith (' he ')) # to determine whether to start with a content#Print (St.expandtabs (tabsize=20))#Print (St.find (' t ')) # finds the first element and returns the index value#Print (St.format (name= ' Alex ', age=37)) # Another way to format the output is to be determined:?: {}#print ({' Name ': ' Alex ', ' Age ': St.format_map} )#Print (St.index (' t '))#print (' ASD '. Isalnum ())#print (' 12632178 '. Isdecimal ())#print (' 1269999.uuuu '. IsNumeric ())#print (' abc '. isidentifier ())#print (' ABC '. Islower ())#print (' ABC '. Isupper ())#print (' E '. isspace ())#print (' My title '. Istitle ())#print (' My tltle '. Lower ())#print (' My tltle '. Upper ())#print (' My tltle '. Swapcase ())#print (' My tltle '. Ljust (+, ' * '))#print (' My tltle '. Rjust (+, ' * '))#print (' \tmy tltle\n '. Strip ())#print (' \tmy tltle\n '. Lstrip ())#print (' \tmy tltle\n '. Rstrip ())#print (' OK ')#print (' My title title '. Replace (' itle ', ' lesson ', 1))#print (' My title title '. RFind (' t '))#print (' My title title '. Split (' I ', 1))#print (' My title title '. Title ())#pick some important string methods#1 Print (St.count (' l '))#Print (St.center (50, ' # ')) # Center#Print (St.startswith (' he ')) # to determine whether to start with a content#Print (St.find (' t '))#Print (St.format (name= ' Alex ', age=37)) # Another way to format the output is to be determined:?: {}#print (' My tltle '. Lower ())#print (' My tltle '. Upper ())#print (' \tmy tltle\n '. Strip ())#print (' My title title '. Replace (' itle ', ' lesson ', 1))#print (' My title title '. Split (' I ', 1))

Class notes: Python Basics-dictionaries

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.