25, python based learning - a collection

. 1  # ! / Usr / bin / Python the env 
2  # __author: HLC 
. 3  # DATE: 2019/6/1 
. 4  
. 5  # SET set 
6  # set to form a set of different elements together, python type foundation when set 
7  # Composition members of the collection can not be repeated 
. 8  # a = [1,2,3,4,5,1,2,3,4,5, "a", "a", "a", "B", "B" " B "," C "] 
. 9  # B = set (A) # keys are defined by a set of set 
10  # Print (A) # [. 1, 2,. 3,. 4,. 5,. 1, 2,. 3,. 4,. 5, 'A', 'A', 'A', 'B', 'BB', 'C'] 
. 11  # Print (B) # {. 1, 2,. 3,. 4,. 5, 'A', 'BB', 'c', 'b'} 
12 is  
13 is  # can hash value collection object a disordered array: set members can do dictionary keys 
14  #a = [[ "asdf", 1244], 1,2,3,4,5,1,2,3,4,5, "a", "a", "a", "b", "b" "B", "C"] 
15  # B = sET (A) # TypeError: unhashable type: 'List' 
16  # Print (B) 
. 17  
18 is  # cLASSIFICATION: set variable, the set of immutable 
19  # variable set, add and remove elements, non-hash, dictionary keys can not be used, the other can not be set as an element, the opposite set of immutable 
20 is  # a = [l, 2,3] 
21 is  # B = sET (a ) # TypeError: unhashable type: 'set' 
22 is  # C = {B: "SDF"} 
23 is  
24  # Create a set of 
25  # because there is no collection of its own syntax, the method can only be set by the factory set () and frozenset ( ) created 
26  # s1 = the SET ( "asfd")
27 # s2 = frozenset("dsg")
28  # Print (S1, type (S1)) # { 'F', 'D', 'A', 'S'} <class' SET '> 
29  # Print (S2, type (S2) # frozenset ({' D ',' G ',' S '}) <class' frozenset'> 
30  
31 is  # access set 
32  # Since the collection itself is disordered, so can not create an index or as a set of slice operation, or only used in loops through , not in access or judged collection elements. 
33 is  # S1 = sET ( "asdfsd") 
34 is  # Print ( "A" in S1) # True 
35  # S1 [0] # TypeError: 'sET' Object IS Not subscriptable 
36  
37 [  # for i in s1:
38 #     print(i)
39 # d
40 # s
41 # a
42 is  # F 
43 is  
44 is  # update the collection, the following can be built using Method: 
45  # S1 = SET ( "asdf") 
46 is  # s1.add () 
47  # s1.update () 
48  # s1.remove () 
49  # only the variable set in order to update 
50  # S1 = frozenset ( "gdsfg") 
51 is  # # s1.add () # AttributeError: 'frozenset' Object attribute has NO 'the Add' 
52 is  
53 is  # S2 = sET ( "SDF") 
54 is  # s2.add # ( "MMN") {# 'F', 'MMN', 'D', 'S'} 
55  # s2.update ( "JKL") {# 'D', 'L','s', 'k', 'j', 'f'}
56 #Print (S2) 
57 is  
58  # S1 = SET ([ "asdf", 325, "456"]) 
59  # # s1.remove (325) # { '456', 'asdf'} 
60  # s1.pop () # { 'asdf', 325} 
61 is  # s1.clear () # sET () 
62 is  # del NameError # S1: name 'S1' iS Not defined 
63 is  # Print (S1) 
64  
65  # collection type operator 
66  # in, in not 
67  # collection is equivalent to not equivalent (==,! =) 
68  # subset of the superset 
69  # Print (the sET ( "asd") == the sET ( "asdasd")) True # 
70  # S = the sET ( "adsfas")
71 #S1 = SET ( "ADS") 
72  # Print ( "S" in S) # True 
73 is  # Print (S> S1) # True 
74  
75  # joint (|) or operating joint (Union) operation with a set of fact and the like price, there is a combined symbol equivalent methods, Union () 
76  # S1 = SET ( "qewr") 
77  # S2 = SET ( "ghkj") 
78  # S3 = S1 | S2 
79  # Print (S3) {# 'E', 'K', 'R & lt', 'H', 'W', 'G', 'Q', 'J'} 
80  # Print (s1.union (S2)) {# 'E', ' K ',' R & lt ',' H ',' W ',' G ',' Q ',' J '} 
81  
82 A = SET ([1,2,3,4,5])
83 b = set([4,5,6,7,8])
84 
85 #Intersection intersrction () is equivalent to B & A 
86  # Print (a.intersection (B)). 4 {#,}. 5 
87  # and union set equal to A | B 
88  # Print (a.union (B)). 1 {# , 2, 3, 4, 5, 6, 7, 8} 
89  # difference current equivalent to the difference A - B 
90  # Print (a.difference (B)) {#. 1, 2, 3} 
91 is  # Print (B. -difference (A)) {#. 8,. 6,}. 7 
92  # symmetric difference (reverse intersection) The symmetric_difference equivalent ^ B A 
93  # Print (a.symmetric_difference (B)) {#. 1, 2,. 3,. 6, 7, 8} 
94  # superset superset 
95  # Print (a.issuperset (B)) #> False 
96  # subset 
97 # print(a.issubset(b)) # < False

 

Guess you like

Origin www.cnblogs.com/hlc-123/p/10960311.html