The world is not made of lists and arrays
The world is not made of lists and arrays
Mathematicians uses sets far more often
The world is not made of lists and arrays
Mathematicians uses sets far more often
An unordered collection of distinct items
The world is not made of lists and arrays
Mathematicians uses sets far more often
An unordered collection of distinct items
Collection: contains zero or more items
The world is not made of lists and arrays
Mathematicians uses sets far more often
An unordered collection of distinct items
Collection: contains zero or more items
Distinct: no item appears more than once
The world is not made of lists and arrays
Mathematicians uses sets far more often
An unordered collection of distinct items
Collection: contains zero or more items
Distinct: no item appears more than once
Unordered: no such thing as "first" or "last"
The world is not made of lists and arrays
Mathematicians uses sets far more often
An unordered collection of distinct items
Collection: contains zero or more items
Distinct: no item appears more than once
Unordered: no such thing as "first" or "last"
- This is the part people tend to trip over most
Sets were added to Python after most of the
language was already defined
Sets were added to Python after most of the
language was already defined
- But at least they're there...
Sets were added to Python after most of the
language was already defined
- But at least they're there...
Python 2.6
primes = set([2, 3,
5])
Sets were added to Python after most of the
language was already defined
- But at least they're there...
Python 2.6 Python 2.7
primes = set([2, 3, primes = {2, 3, 5}
5])
Sets were added to Python after most of the
language was already defined
- But at least they're there...
Python 2.6 Python 2.7
primes = set([2, 3, primes = {2, 3, 5}
5])
empty = set()
empty = set()
Sets were added to Python after most of the
language was already defined
- But at least they're there...
Python 2.6 Python 2.7
primes = set([2, 3, primes = {2, 3, 5}
5])
empty = set()
empty = set()
Because {} was already used for something else
Sets were added to Python after most of the
language was already defined
- But at least they're there...
Python 2.6 Python 3.1
primes = set([2, 3, primes = {2, 3, 5}
5])
empty = set()
empty = set()
Because {} was already used for something else
We'll use Python 2.7 notation in this lecture
Naturally used to find unique items in a collection
Naturally used to find unique items in a collection
# What letters are used?
letters = set()
for char in 'ichthyosaur':
letters.add(char)
print letters
set(['a', 'c', 'i', 'h', 'o', 's', 'r', 'u', 't', 'y'])
Naturally used to find unique items in a collection
# What letters are used?
letters = set()
for char in 'ichthyosaur':
letters.add(char)
print letters
set(['a', 'c', 'i', 'h', 'o', 's', 'r', 'u', 't', 'y'])
Not ordered alphabetically or by order of addition
Naturally used to find unique items in a collection
# What letters are used?
letters = set()
for char in 'ichthyosaur':
letters.add(char)
print letters
set(['a', 'c', 'i', 'h', 'o', 's', 'r', 'u', 't', 'y'])
Not ordered alphabetically or by order of addition
Because set elements are not ordered
A much shorter way to accomplish the same goal
A much shorter way to accomplish the same goal
# What letters are used?
print set('ichthyosaur')
set(['a', 'c', 'i', 'h', 'o', 's', 'r', 'u', 't', 'y'])
A much shorter way to accomplish the same goal
# What letters are used?
print set('ichthyosaur')
set(['a', 'c', 'i', 'h', 'o', 's', 'r', 'u', 't', 'y'])
If you can loop over it, you can build a set from it
A much shorter way to accomplish the same goal
# What letters are used?
print set('ichthyosaur')
set(['a', 'c', 'i', 'h', 'o', 's', 'r', 'u', 't', 'y'])
If you can loop over it, you can build a set from it
Can not build a set from several separate items
A much shorter way to accomplish the same goal
# What letters are used?
print set('ichthyosaur')
set(['a', 'c', 'i', 'h', 'o', 's', 'r', 'u', 't', 'y'])
If you can loop over it, you can build a set from it
Can not build a set from several separate items
set('a', 'e', 'i', 'o', 'u')
TypeError: set expected at most 1 arguments, got 5
>>> ten = set(range(10)) # {0...9}
>>> lows = {0, 1, 2, 3, 4}
>>> odds = {1, 3, 5, 7, 9}
>>> ten = set(range(10)) # {0...9}
>>> lows = {0, 1, 2, 3, 4}
>>> odds = {1, 3, 5, 7, 9}
# add an element
>>> lows.add(9)
>>> lows
set([0, 1, 2, 3, 4, 9])
>>> ten = set(range(10)) # {0...9}
>>> lows = {0, 1, 2, 3, 4}
>>> odds = {1, 3, 5, 7, 9}
# add an element
>>> lows.add(9)
>>> lows
set([0, 1, 2, 3, 4, 9])
# remove all elements
>>> lows.clear()
>>> lows
set()
# difference
>>> lows.difference(odds)
set([0, 2, 4])
# difference
>>> lows.difference(odds)
set([0, 2, 4])
# intersection
>>> lows.intersection(odds)
set([1, 3])
# difference
>>> lows.difference(odds)
set([0, 2, 4])
# intersection
>>> lows.intersection(odds)
set([1, 3])
# subset
>>> lows.issubset(ten)
True
# superset
>>> lows.issuperset(odds)
False
# superset
>>> lows.issuperset(odds)
False
# remove an element
>>> lows.remove(0)
>>> lows
set([1, 2, 3, 4])
# superset
>>> lows.issuperset(odds)
False
# remove an element
>>> lows.remove(0)
>>> lows
set([1, 2, 3, 4])
# symmetric difference (also called "exclusive or")
>>> lows.symmetric_difference(odds)
set([2, 4, 5, 7, 9])
# union
>>> lows.union(odds)
set([1, 2, 3, 4, 5, 7, 9])
# union
>>> lows.union(odds)
set([1, 2, 3, 4, 5, 7, 9])
# size
>>> len(odds)
7
# union
>>> lows.union(odds)
set([1, 2, 3, 4, 5, 7, 9])
# size
>>> len(odds)
7
# membership
>>> 6 in odds
False
Methods Operators
lows.difference(odds) lows - odds
lows.intersection(odds) lows & odds
lows.issubset(ten) lows <= ten
lows < ten
lows.issuperset(ten) lows >= odds
lows > odds
lows.symmetric_difference(o lows ^ odds
dds)
lows | odds
lows.union(odds)