14.python A summary of the set, the string formatted

Data type classification

Variable immutable:
  1. Variable: List, Dictionary
  2. immutable: string, number, tuple
access order:
  1. Direct Access: Digital
  2. sequential access: strings, lists, tuples
  3. Mapping: dictionary
storage number of elements:
  1. type containers: lists, tuples, dictionaries
  2 atomic types: numeric, string

set

By different sets of elements, the set is a set of hash values disordered array can be used as a dictionary Key
1. different elements
2. unordered
3. The elements in the set must be immutable type, such as: string digital tuple
set of definitions:

s={1,2,5,6,7,3,5}
for i in s:
    print(i)
>>>
1
2
3
5
6
7

set definitions:

s=set('hello')
print(s)
# >>>{'o', 'l', 'e', 'h'}
s=set(['raito','raito','nb'])
print(s)
# >>># {'nb', 'raito'}

method:

1,2,3,4,5,6 = {S }
 # Add 
s.add ( ' S ' ) 
s.add ( ' . 3 ' ) 
s.add ( . 3 )
 Print (S) # { '. 3',. 1 , 2,. 3,. 4,. 5,. 6, 'S'} 
# empty 
# s.clear () 
# Print (S) #set () 
# copies 
S1 = s.copy ()
 Print (S1) # {. 1, 2 ,. 3,. 4,. 5,. 6, '. 3', 'S'} 
S = { ' A ' , ' B ' , 1,2,3,4,5,6 }
 # random puncturing 
# s.pop ()
# Print (S) 
# Specifies to delete 
# s.remove ( 'b') 
# s.remove ( 'hellol') # remove an element does not exist will get an error 
# s.discard ( 'ASAS') # remove an element does not exist without error 
# Print (S)

Set operations (common operation):

python_l = [ ' LCG ' , ' SZW ' , ' ZJW ' , ' LCG ' ] 
linux_l = [ ' LCG ' , ' SZW ' , ' SB ' ] 
P_s = SET (python_l) 
L S = SET (linux_l)
 # intersection 
Print ( P_s, L S)
 Print (p_s.intersection (L S))
 Print (& P_s L S)
 # and set 
Print (p_s.union (L S))
 Print(P_s | L S)
 # set difference: I have you do not have a 
Print ( ' set difference ' , p_s- L S)
 Print (p_s.difference (L S))
 Print ( ' set difference ' , l_s- P_s)
 Print (l_s.difference (P_s))
 # deduplication portion of the rear set of requirements and set: cross complement 
Print ( ' cross complement ' , p_s.symmetric_difference (L S))
 Print ( ' cross complement ' , P_s ^ L S)

 

Guess you like

Origin www.cnblogs.com/raitorei/p/11668892.html