(1) List
1 # -*-coding:utf-8-*- 2 # !/usr/bin/env python 3 # Author:@vilicute 4 # 创建列表 5 myList = [x for x in range(10) if not x % 2] # myList = [0, 2, 4, 6, 8] 6 myList1 = ["aaa", "bbb", "ccc", "ddd", "eee", "fff"] 7= myList2 [ " GGG " , " HHH " , " III " , " JJJ " ] . 8 # list operations . 9 Print (myList [2], myList [-2], myList1 [:. 3], myList1 [-3: -1 ], myList1 [. 1:. 4 ]) 10 myList1.insert (. 1, " liangba " ) # at the specified position of the insert elements . 11 myList1.append ( " XXX " ) # at the end of the insertion element 12 is myList1.remove ( " XXX " ) #Delete an element 13 del myList1 [1] # deleted by the subscript element 14 myList1.pop () # default without deleting the last index, have developed position is deleted, for example: myList1.pop (1) 15 myList1. COUNT (myList1 [2]) # count the number of the same item 16 myList1.reverse () # inversion . 17 XX = myList1.index ( " CCC " ) # location query 18 is myList1.sort () # sorted alphabetically . 19 myList1. Extend (myList2) # list merge 20 del myList2 # delete myList2
(2) tuple
Unlike lists, tuples, once created, will not be able to tuple elements to add, delete, replace, or reorder, etc, but you can access, you can read but not operational.
. 1 # - * - Coding: UTF-. 8 - * - 2 # ! / Usr / bin / the env Python . 3 # the Author: @vilicute . 4 # created tuples . 5 tuple1 = (. 1, 2,. 3) # Create a tuple . 6 Tuple2 tuple = ([X for X in Range (. 1, 10, 2)]) # Create a list of tuples . 7 tuple3 = tuple ( " ABCDE " ) # create tuples string . 8 Print ( " tuple1 [2] = " , tuple1 [2], " \ ntuple2 = " , Tuple2, " \ ntuple3 =" , Tuple3) # can be accessed by index . 9 '' ' 10 tuple1 [2] =. 3 . 11 Tuple2 = (. 1,. 3,. 5,. 7,. 9) 12 is tuple3 = (' A ',' B ',' C ' , 'D', 'E') 13 is ' '' 14 Len = len (tuple3) # Element Group Length 15 Max = max (Tuple2) # maximum 16 Min = min (Tuple2) # Min . 17 the Sum = SUM (Tuple2) # sum 18 is tuple4 Tuple2 = 2 * # copy extension . 19 tuple5 + = tuple1 Tuple2 # tuple connector 20 is List List = (Tuple2) # 转为列表 21 print(" Max = ",Max, " Min = ",Min, " Sum = ", Sum, '\n', "tuple4 = ", tuple4, "\ntuple5 = ", tuple5, "\nList = ", List) 22 ''' 23 Max = 9 Min = 1 Sum = 25 24 tuple4 = (1, 3, 5, 7, 9, 1, 3, 5, 7, 9) 25 tuple5 = (1, 2, 3, 1, 3, 5, 7, 9) 26 List = [1, 3, 5, 7, 9] 27 ''' 28 print('Tuple =',2*tuple([x for x in range(10) if not x%2])) 29 # Tuple = (0, 2, 4, 6, 8, 0, 2, 4, 6, 8)
(3) dictionary
Form: dict = {Keyword 1: 1 key, 2 key: the key value 2, ...}, the dictionary can not contain duplicate keywords!
1 # -*-coding:utf-8-*- 2 # !/usr/bin/env python 3 # Author:@vilicute 4 dict1 = {"1123": "Zhangsan", "1124": "Lisi", "1125": "Wangwu"} 5 dict1["1126"] = "Zhaoliu" dict1: inKeyfor6Add an entry# # Access . 7 Print (Key + ' : ' + STR (dict1 [Key])) . 8 # len, ==, = slightly! 9 # dictionary method: 10 an xz = tuple (dict1.keys ()) # key string 11 tuple = XX (dict1.values ()) # value sequence 12 is XC = tuple (dict1.items ()) # tuples 13 is XV dict1.get = ( " 1126 " ) # key value corresponding to 14 XB = dict1. POP ( " 1123 " ) # delete keywords corresponding entry and return its value 15dict1.popitem = Xn () # returns a random key tuple and delete the selected entry 16 del dict1 [ " 1124 " ] # delete the corresponding entries . 17 dict1.clear () # delete all entries 18 is Print ( " an xz = " , an xz, ' \ n- ' , " XX = " , XX, ' \ n- ' , " XC = " , XC, ' \ n- ' , " XV = " , XV, ' \ n- ' ,"XB = " , XB, ' \ n- ' , " Xn = " , Xn) . 19 '' ' 20 is an xz = (' 1123 ',' 1124 ',' 1125. ',' 1126 ') 21 is XX = (' Zhangsan ', 'Lisi', 'wangwu', 'Zhaoliu') 22 is XC = (( '1123', 'Zhangsan'), ( '1124', 'Lisi'), ( '1125.', 'wangwu'), ( '1126' , 'Zhaoliu')) 23 is XV Zhaoliu = 24 XB = Zhangsan 25 Xn = ( '1126', 'Zhaoliu') 26 is ' ''
(4) collection
Features: no duplicate elements and disorderly.
. 1 # - * - Coding: UTF-. 8 - * - 2 # ! / Usr / bin / the env Python . 3 # the Author: @vilicute . 4 # create a collection, creating a similar method tuple . 5 Set1 = SET () . 6 Set2 = {. 1, 2,. 3,. 4 } . 7 Set3 = SET ([X for X in Range (. 1, 10, 2 )]) . 8 Set4 = SET ( " ABCDE " ) . 9 SET5 = SET ((2,. 3 )) 10 = list list (Set4) # Switch list 11 # operations set 12 is # len (), max (), min (), SUM (), slightly 13 is Set3.add (. 8) # add an element 14 Set3.remove (. 5) # delete * a * presence element 15 TF = (== Set3 Set4) # test for equality or False Ture 16 Sub = Set3.issubset (Set1 ) # Set3 whether Set1 subset or False Ture 17 Sup = Set3.issuperset (Set2) # Set3 whether Set2 is a superset of Ture or False Ture: Set2 belong Set3 18 of union = Set1 | Set2 # union == Set1. Union (Set2) . 19 intersection = Set1 Set2 & # intersection Set1.intersection == (Set2) 20 is difference = Set1 - Set2 # difference set Set1.difference == (Set2) 21 is xxx = Set1 ^ Set2 # 异或 == Set1.symmetric_difference(Set2)