Study notes (12): Do you really understand Python entry to actual combat-dictionaries and collections? (three)

Learn now: https://edu.csdn.net/course/play/26676/338782?utm_source=blogtoedu

Collection: Variable collection, immutable collection, characteristics of collection.

Define a collection: set()

{1, 2, 3, 4}, the sets are different. The elements in the collection must be immutable objects.

The elements in the set are in no order.

Variable collection:

s.add (element), add an element to the collection.

s.pop(), delete elements in the collection.

s.remove (specify element). Delete the specified element in the collection.

s.discard (specify element). You can delete specified elements that are not in the collection.

Immutable collection:

frozenset(), no order, no repetition.

set.copy Shallow copy.

b2=b1.copy.

b1 and b2 are two different objects in memory. Use b1 is b2 to judge.

b1 and b2 are not the same object, but the elements inside are the same object.

Shallow copy: Copy the first layer of the container. If there is a second container, it is still the same object.

Dictionaries, lists, and collections are all shallow copies.

A completely separated copy is a deep copy.

import copy

copy.deepcopy (object to be copied)

To determine whether the element is in the set, use in.

Determine whether a set is a superset or a subset of another set.

a.issuperset(b) Determine whether set a is a superset of set b.

b.issubset(a) Determine whether set b is a subset of set a.

Union of a and b:

a|b or a.union(b)

The intersection of a and b:

a&b or a.intersection(b)

Difference of a and b:

ab or a.difference(b)

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_49939521/article/details/108574489