Electron microscopy
 
PythonML
Cheatsheet of Sets
- Python Automation and Machine Learning for ICs -
- An Online Book: Python Automation and Machine Learning for ICs by Yougui Liao -
Python Automation and Machine Learning for ICs                                                           http://www.globalsino.com/ICs/        


Chapter/Index: Introduction | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | Appendix

=================================================================================

In Python, sets are unordered collections of unique elements:

  • Unordered: The elements in a set do not have a defined order; they cannot be referred to by index or sequence like elements in lists or tuples.
  • Unique Elements: Sets automatically remove duplicate entries. Only one instance of each unique element is stored, no matter how many times it is added.

This makes sets particularly useful for operations involving membership testing, removing duplicates from a sequence, and performing mathematical operations like unions, intersections, differences, and symmetric differences.

Table 3288. Cheatsheet of sets.

Package/Method Description  Code Example
{'a', 'b'} & {'a'} The operation shown, {'a', 'b'} & {'a'}, represents the intersection of two sets. The intersection includes only the elements that are present in both sets. Output: {'a'}
add() Elements can be added to a set using the `add()` method. Duplicates are automatically removed, as sets only store unique values. set_name.add(element)
clear() The `clear()` method removes all elements from the set, resulting in an empty set. It updates the set in-place. set_name.clear()
copy() The `copy()` method creates a shallow copy of the set. Any modifications to the copy won't affect the original set. new_set = set_name.copy()
Defining Sets A set is an unordered collection of unique elements. Sets are enclosed in curly braces `{}`. They are useful for storing distinct values and performing set operations.

empty_set = set() #Creating an Empty Set

fruits = {"apple", "banana", "orange"}

discard() Use the `discard()` method to remove a specific element from the set. Ignores if the element is not found. set_name.discard(element)
issubset() The `issubset()` method checks if the current set is a subset of another set. It returns True if all elements of the current set are present in the other set, otherwise False. is_subset = set1.issubset(set2)
issuperset() The `issuperset()` method checks if the current set is a superset of another set. It returns True if all elements of the other set are present in the current set, otherwise False. is_superset = set1.issuperset(set2)
pop() The `pop()` method removes and returns an arbitrary element from the set. It raises a `KeyError` if the set is empty. Use this method to remove elements when the order doesn't matter. removed_element = set_name.pop()
remove() Use the `remove()` method to remove a specific element from the set. Raises a `KeyError` if the element is not found. set_name.remove(element)
set1.intersection(set2) Finds the intersection of two sets

# Define two sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

# Find the intersection of the sets
intersection = set1.intersection(set2)

# Print the result
print("The intersection of the two sets is:", intersection)

# Output:
The intersection of the two sets is: {4, 5}

.union() To combine all elements from both sets, eliminating any duplicates.

set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

union_result = set1.union(set2)
print("The union of the two sets is:", union_result)

The union of the two sets is: {1, 2, 3, 4, 5, 6, 7, 8}

Set Operations Perform various operations on sets: `union`, `intersection`, `difference`, `symmetric difference`.

union_set = set1.union(set2)

intersection_set = set1.intersection(set2)

difference_set = set1.difference(set2)

sym_diff_set = set1.symmetric_difference(set2)

update() The `update()` method adds elements from another iterable into the set. It maintains the uniqueness of elements.

set_name.update(iterable)

fruits.update(["kiwi", "grape"]

 

         

===========================================

         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         

 

 

 

 

 



















































 

 

 

 

 

=================================================================================