A collection of Getting Started with Python

Keywords: Python

Collection of Getting Started with Python


1. What is a collection

In addition to lists, tuples, and dictionaries, collections are also one of the built-in data structures provided by the Python language and can be thought of as dictionaries that do not store value s, so they are characterized as follows:

1. Duplicate data cannot be stored in a collection;

2. The data in the collection is out of order;

3. Data in a collection can be of any immutable type, and multiple types of data can be mixed and stored in a collection.

4. Collections can scale dynamically as needed, that is, the system allocates and reclaims memory dynamically as needed, so there is no need to declare the capacity of the collection before use;

5. Collections waste a lot of memory, swapping space for time compared to lists;


2. Creation of Collections

1. Create a collection using curly brackets {}

s = {3,5,8,1,2}
print(s) # {1, 2, 3, 5, 8}
s = {3,3,6,8,8,9}
print(s) # {8, 9, 3, 6}
s = {}
print(type(s)) #<class'dict'> cannot create an empty collection with {}, it is a dictionary


2. Create a collection using the built-in function set

s = set(range(1,6)) # {1, 2, 3, 4, 5}
s = set([3,5,8,1,2]) #{1, 2, 3, 5, 8}, can be passed into the list
s = set((3,5,8,1,2)) #{1, 2, 3, 5, 8}, can be passed in tuples
s = set({3,5,8,1,2}) #{1, 2, 3, 5, 8}, can pass in the {} collection
s = set('35812') #{'5','8','3','2','1'}, incoming strings will be delimited
s = set() #set() <class'set'> Create an empty set


3. Addition or deletion of collections

1. Check operation

Use "in" "not in" to look up lists

s1 = {'a','b','c'}
print('a' in s1) # True
print('d' not in s1) # True


2. Additional operations

Add elements using add and update methods

s1 = {'a','b','c'}
s1.add('d') #Add one element at a time
s1.update('e','f','g') #Add more than one element at a time
print(s1)  # {'g', 'f', 'd', 'b', 'c', 'a', 'e'}


3. Deletion

Delete a collection using pop,remove,discard,clear

s1 = {1,2,3,4,5}
print(s1.pop())  #1. Randomly delete one element at a time
s1.remove(3) #Delete element 3
print(s1) # {2, 4, 5}
s1.discard(2)  #Delete element 2
print(s1) #  {4, 5}
s1.clear() #Empty Collection
print(s1) # set()


IV. Relationship between Sets

1. If two sets are equal, you can use the operator==and!=to make a decision.

s1 = set([1,2,3,4])
s2 = set([2,3,4,5])
print(s1 == s2) # False
print(s1 != s2) # True


2. Is one set a subset of another

#Call method issbubset for judgment
s1 = {1,2,3,4}
s2 = {3,4,6}
s3 = {1,2,3,4,5}
print(s1.issubset(s3)) # True
print(s2.issubset(s3)) # False


3. Is one set a superset of another

#You can call the method issuperset for judgment
print(s3.issuperset(s1)) # True
print(s3.issuperset(s2)) # False


4. Is there no intersection between two sets

#The method isdisjiont can be called for judgment
s1 = {1,3,5,7}
s2 = {22,11,44}
s3 = {1,2,3,5,7,8}
print(s1.isdisjoint(s2)) # True
print(s1.isdisjoint(s3)) # False


5. Mathematical Operation of Sets

1. Intersection of two sets

#Intersection of s1 and s2
#Using the intersection method, the intersection of the two is returned
#Using the s1.intersection_update(s2) method, the return value is none, but the value of the intersection is assigned to S1
s1 = {1,2,3,4}
s2 = {3,4}
print(s1.intersection(s2))  #{3, 4}
print(s1 & s2 ) #{3, 4}
print(s1.intersection_update(s2))  #None
print(s1)  #{3, 4}


2. The union of two sets

Union, union, Union
s1 = {1,2,3,4,5}
s2 = {3,4,5}
print(s1.union(s2)) #{1, 2, 3, 4, 5}
print(s1 | s2 ) #{1, 2, 3, 4, 5}


3. The difference of two sets

#Both elements that s1 does not have in s2 were found
#Using the difference() method to find the difference set
#Using the s1.difference_update(s2) method, the value returned is None, which assigns the difference set to S1
s1 = {1,2,3,4,5,6}
s2 = {1,7,9,4,10}
print(s1 - s2 ) #{2,3,5,6}
print(s1.difference(s2)) #{2,3,5,6}
print(s1.difference_update(s2)) #None
print(s1) #{2, 3, 5, 6}


4. Symmetric difference set of two sets

Symmetric difference, removing the intersection of two sets
s1 = {1,3,5,7,9}
s2 = {2,3,6,7,10}
print(s1 ^ s2 ) #{1, 2, 5, 6, 9, 10}
print(s1.symmetric_difference(s2)) #{1, 2, 5, 6, 9, 10}
print(s1.symmetric_difference_update(s2)) #None
print(s1) #{1, 2, 5, 6, 9, 10}


6. frozenset immutable set

Frozenset, frozen set, is also an immutable set,frozenset in a set is like tuple in a list

frozenset is an immutable type with the following characteristics:

There is a hash value

_A key that can be used as a dictionary

Elements that can be set

#Built-in function frozenset can be called to create
print(frozenset(range(6))) # frozenset({0, 1, 2, 3, 4, 5})
print(frozenset([1,2,5,7])) # frozenset({1, 2, 5, 7})
print(frozenset(('a','b','c'))) # frozenset({'a', 'c', 'b'})
print(frozenset('abcdef')) # frozenset({'c', 'f', 'e', 'a', 'b', 'd'})

#Element as set
s1 = set(frozenset([1,2,3,4,56,7,8]))
print(s1,type(s1))  # {1, 2, 3, 4, 7, 8, 56} <class 'set'>
s1.add(('abc'))
print(s1,type(s1)) {1, 2, 3, 4, 'abc', 7, 8, 56} <class 'set'>


Posted by JohnResig on Tue, 07 Jan 2020 09:43:17 -0800