In Python, a set is a commonly used composite data type. A set is an unordered collection of unique elements. Unlike lists and tuples, elements in a set are unordered, and each element can only appear once.
This document mainly introduces how to use Python for Object Oriented programming, and requires readers to have a basic understanding of Python syntax and microcontroller development. Compared with other blogs or books that explain Python Object Oriented programming, this document is more detailed and focuses on embedded host computer applications. It takes the common serial port data transmission and reception, data processing, dynamic graph drawing and other functions between the host computer and the slave computer as application examples, and uses the Sourcetrail code software to visually read the code to facilitate readers' understanding.
The link to obtain the relevant sample code is as follows:
https://github.com/leezisheng/Python-OOP-Demo
Main Text
Python also supports the set data type. A set is an unordered container consisting of unique elements. Its basic uses include membership testing and eliminating duplicate elements. Set objects support mathematical operations such as union, intersection, difference, and symmetric difference. You can create a set using curly braces or the set () function. Note that to create an empty set, you can only use set () instead of {}, as {} creates an empty dictionary.
In Python, a set can hold any hashable object, not just numbers. A hashable object is the same kind of object that can be used as a key in a dictionary, so lists and dictionaries are excluded.
Like mathematical sets, they only store a backup of objects. Just like dictionaries, sets are unordered. Both leverage underlying hash-based data structures to ensure efficiency. Since they are unordered, elements in a set cannot be looked up via an index. The primary purpose of using a set is to divide the world into two parts: "those that exist in the set" and "those outside the set". It is easy to check whether an item is in the set or iterate over all items in the set, but if you want to sort them, you must first convert the set into a list.
Here are some sample codes for collection-related operations:
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)
# Check whether a member is in the set
print('orange' in basket)
print('crabgrass' in basket)
# Create two empty sets
a = set('abracadabra')
print(a)
b = set('alacazam')
print(b)
# Letters in a but not in b
print(a - b)
# Letters in a or b
print(a | b)
# Letters in both a and b
print(a & b)
# Letters in a or b but not in both
print(a ^ b)
The following are the running results:
Here we list some of the operation methods for sets for readers' reference:
Method
Description
add()
Add elements to the collection.
clear()
Removes all elements from the collection.
copy()
Copy a set.
difference()
Returns the difference set of multiple sets.
difference_update()
Removes the elements from the set that are also present in the specified set.
discard()
Removes the specified element from the collection.
intersection()
Returns the intersection of the sets.
intersection_update()
Returns the intersection of the sets.
isdisjoint()
Determine whether two sets contain the same elements; return True if they do not, and False otherwise.
issubset()
Determine whether the specified set is a subset of the parameter set of this method.
issuperset()
Determine whether the parameter set of this method is a subset of the specified set.
pop()
Randomly remove an element.
remove()
Removes the specified element.
symmetric_difference()
Returns a set of the elements that are not duplicated in the two sets.
symmetric_difference_update()
Removes from the current set any elements that are present in another specified set, and inserts into the current set the elements that are present in the other specified set but not in the current set.
union()
Returns the union of two sets.
update()
Add elements to the collection.
len()
Calculate the number of elements in a set.
It is evident from the operation methods of sets that sets are designed for performing operations with other sets, rather than merely serving as a container. If you have data from two different sources and need to quickly combine them in a certain form to determine whether the data overlaps or differs, you can use set operations to compare them efficiently. Alternatively, if the input data may contain duplicate processed data, you can use sets to compare them and only process the new data.
Similar to lists, sets also support comprehensions, however when using the `in` keyword to check whether an element exists in a container, sets are more efficient than lists. When using the syntax `value in container` on a list or set container, it returns True if there is an element in the container equal to `value`, otherwise it returns False. In a list, this will iterate through all objects until a matching value is found, whereas for a set, it only needs to compute the hash value and check for its existence.This means that no matter how large the set is, the time required to check whether a value exists is fixed, whereas for a list, the longer the list length, the longer the time required to search for a value.