onlyforbopi
9/30/2019 - 5:55 PM

## Python.DataTypes.Sets

#python #data #datatype #set #lookup

Sets are like lists, but unordered. They are perfectly suited for lookup of
values.

- BasicSetFunctions.py      : List the basic set functions.
def pretty_print(input, indent_depth, compact = False):
'''
Name: pretty_print
Description: pretty_prints a data structure
Input: <List/Tuple/Dict>, indent_depth
Output: stdout
Usage: pretty_print(dict_in, 4, T/F)
Notes: Works on any kind of data structure.
Requires: pprint module
'''
import pprint
try:
pp = pprint.PrettyPrinter(indent=indent_depth, compact=compact)
pp.pprint(input)
#pprint.PrettyPrinter(indent=indent_depth)
except:
print("Pretty print failed")

# Set definition -> List[]/Set{}
set1 = {1, 2, 3}
set2 = set('qwerty')
set3 = set()
set4 = [ 'this', 'is', 'a', ['and', 'a', 'sublist', 'too'], 'list', 'including', 'many', 'words', 'in', 'it']

# pretty_print(set1, 4)
# pretty_print(set4, 4, False)

# Basic operators (Iteratior, Addition, Removal, Check)
set_primes = { 2, 3, 5, 7, 11 }
for num in set_primes:
print(num)

set_primes.remove(13)
print(True if 2 in set_primes else False)
if 4 in set_primes:
print(False)
else:
print(True)

# removing elements
set3.remove(4) # will throw keyerror, use discard

# remove a random element
set3.pop() # key error if the set is empty.

# Union A|B
print(set1.union(set2))

# Update A|=B adds all elements of B to A
set3.update(set2)
print(set3)

# Intersection
print(set3.intersection(set2))

# Intersection update
set3.intersection_update(set2)
print(set3)

# Difference
set3.difference(set2)
print(set3)

# Difference_update
set3.difference_update(set2)

# Symmetric Difference
set3.symmetric_difference(set2)

# Symmetric Difference Update
set3.symmetric_diference_update(set2)

# Subset
set3.issubset(set2)

# Superset
set3.issuperset(set2)

set3 < set2
set3 > set2