Python learns the second day of lists, dictionaries and nesting

Source: Internet
Author: User
Tags shallow copy

1 #!/user/bin/env python2 #-*-coding:utf-8-*-3 __author__='Howie'4names = ['Zhangsan','LiSi','Wangwu',['Lili','dddd'],'Zhaoliu','DAda']5 #A = names[-2:] #如果想取倒数几个数需要吧最后一个省略6A = Names[:3]#If you want to take the first start to the number of numbers you can omit the index value7 Print(A)8Names.append ('Lili')#Append9 Print(names)TenName2 =names.copy () One Print('after a shallow copy:', name2) ANAMES[2] ='Harry' -Names[3][0] ='LILI' - Print('Original list:', names) the Print('the sub-list of the original list is modified after a shallow copy: the', name2) -  - #to import a module if you want to copy it completely - ImportCopy +Name2 =copy.copy (names) - Print('Original list:', names) + Print('after a full copy:', name2) A  at  -  - #Names.pop (1) #删除第二个 - #del names[1] #和上面的差不多一样的 -Names.remove ('LiSi')#Match Delete - Print('Delete', names) inNames.insert (1,'DAda')#Intermediate Insert Value Ask index value - Print(names) toNAMES[2] ='Xiugai' #change is to wait for a second assignment + Print(names) - Print('Query value at index location:', Names.index ('DAda')) the Print('statistics Number of names:', Names.count ('DAda')) * Names.reverse () $Names2 = [' the','3','6','8','1','5']Panax Notoginseng names.extend (names2) - Print('Expansion:', names) the  + #Names.sort () A #print (' sort: ', names) the  +  -  $  $ names.clear () - Print(names)
useful ways to list
1 #!/user/bin/env python2 #-*-coding:utf-8-*-3 __author__='Howie'4list_1 = [1,5,8,6,2,4,3,2,5,1,1,6]5List_2 = Set ([1,5,5,1,1,6,44,11,22])6List_1 =Set (list_1)7 Print(list_1,list_2)8 Print('The intersection can also be done with &:', List_1.intersection (list_2))9 Print('The intersection can also be done with &:', List_1 &list_2)Ten Print('and set:', List_1.union (list_2)) One Print('The set can also be used | To do:', list_1|list_2) A Print('difference set:', List_1.difference (list_2))#1 have but 2 not. - Print('The difference can also be used-to do:', list_1-list_2)#1 have but 2 not. - Print('symmetric difference set:', List_1.symmetric_difference (list_2))#a collection other than intersection the Print('symmetric difference Sets can also be done with ^:', list_1^list_2)#a collection other than intersection -  -List_3 =list_1.union (list_2) - Print('subset:', List_1.issubset (list_2), List_1.issubset (List_3))#Judging is not a subset + Print('Parent Set:', List_3.issuperset (list_1)) -List_4 = Set ([333,5555,66666]) + Print('judge whether there is no intersection if it is not true:', List_1.isdisjoint (list_4)) AList_4.add (44444) atList_4.update ([11111,2222,66,333]) -List_4.remove (333) - Print(List_4)
practical ways to set up
1 #!/user/bin/env python2 #-*-coding:utf-8-*-3 __author__='Howie'4info = {5     'Zhangsan': 500,6     'Lisi': 600,7     'Zhaowu': 7008 }9b = {Ten     'Lisi': 789, One1:2, A3:4 - } - " " the Print (type (info)) - Print (info) - print (info[' Lisi ') - info[' lisi '] = ' John Doe ' + Print (info) - info[' Wangwu '] = ' Harry ' + Print (info) A del info[' Zhaowu '] at Print (info) - Print (Info.get (' Lili ')) - print (' Lili ' in info) -  - info.setdefault (' Lisi ', {456}) - Print (info) in  - Info.update (b) to " " +A =Info.items () -A = Info.fromkeys ([7,8,9]) the  * PrintA
the practical way of dictionaries

Python learns the second day of lists, dictionaries and nesting

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.