A collection of file read and write +

Define two sets:

a = {1, 2, 3, 4, 5, 6, 5, 4}   

b = {5, 6, 7, 8, 9}

 

1. Features a collection of

Collection of natural de-emphasis, and unordered (ie no subscript), relationship testing can be done

 

2. Increase

a.add(10)  

 

3. Modify

Collection need to put value into a list to be modified

a.update([333])

 

4. Delete

A. the Remove ( 777 )  # delete elements, will get an error if the element does not exist

A. POP ( )  # delete a random element, and returns the removed elements

A. discard ( 'dddd' )  # If you remove the element exists, delete, there is no processing

 

 

5. intersected

print (a.intersection (b)) # intersected

print (a & b) # intersected

 

6. taking the difference current

print (a.difference (b)) # difference set, which is present in a set, but there is no collection of b

print (a - b) # difference set, which is present in a set, but there is no set of b

 

7. taken and set

print (a.union (b)) # and set, the two sets are merged together, then remove the duplicate

print (a | b) # and set, the two sets are merged together, then remove the duplicate

 

8. The symmetric difference

print (a.symmetric_difference (b)) the output values ​​are not both lists, i.e. the same two sets removed

Guess you like

Origin www.cnblogs.com/wangyujian/p/11526393.html