This article focuses on some simple operations for collections
Sets are divided into mutable sets and immutable sets
Variable set (non-hash)
S={1,'a', (+/-),} # equals set ({1, ' a ', (+/-),})
Add Value
S={1,'a', (All-you-do),}print(s) s.add ('123 ' )print(s)
Clear Collection
S={1,'a', (+/-),}print(s) s.clear () Print(s)
Copy Collection
S={1,'a', (A/b),}print(s) a=s.copy ()Print (a)
Random Delete
S={1,'a', (+/-),}print(s) s.pop ()Print (s)
Specify delete element
S={1,'a', (A/b),}print(s) s.remove (1)Print (s)
Non-repeatable properties
Immutable collections
Less use
Calling the Frozenset method
F=frozenset ({1,2,3,4})
When you're done, you'll find no F.add,f.pop,f.remove method.
Variable Collection Common operations
Example:
python_list=['Tom','Jack','Lucy']openstact_list=['Rose','Tom','xiaoming','Lucy']p_u=Set (python_list) O_u=set (Openstact_list)#define a list as a collection
Subtraction
# learn only OpenStack courses without learning python lessons, and vice versa Res=o_u- p_uprint(res)
and set:
Res=p_u | O_u Print (RES)
Intersection:
Res=p_u & O_uprint(res)
Complementary set:
Res=p_u ^ O_uprint(res)
Parameter operation:
Print (P_u.difference (O_u)) # Straight Results p_u.difference_update (O_u)# Find the results and update print(P_u)
P_u.discard ('Tom') # If the collection is removed, nothing is done print(P_u) P_u.discard ('asdasdasd')print(P_u)
Print (P_u.intersection (O_u)) # intersection # Output and update
Res=p_u.union (O_u) # and set print(res)
# Cross complement set Print (RES)
Python first day-Getting Started Python (5) Collection operations