storage copyright © software carpentry 2010 this work is licensed under the creative commons...
TRANSCRIPT
![Page 1: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/1.jpg)
Storage
Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.
Sets and Dictionaries
![Page 2: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/2.jpg)
Sets and Dictionaries Introduction
Let's try an experiment
![Page 3: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/3.jpg)
Sets and Dictionaries Introduction
>>> things = set()
>>> things.add('a string')
>>> print things
set(['a string'])
Let's try an experiment
![Page 4: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/4.jpg)
Sets and Dictionaries Introduction
>>> things = set()
>>> things.add('a string')
>>> print things
set(['a string'])
>>> things.add([1, 2, 3])
TypeError: unhashable type: 'list'
Let's try an experiment
![Page 5: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/5.jpg)
Sets and Dictionaries Introduction
>>> things = set()
>>> things.add('a string')
>>> print things
set(['a string'])
>>> things.add([1, 2, 3])
TypeError: unhashable type: 'list'
Let's try an experiment
What's wrong?
![Page 6: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/6.jpg)
Sets and Dictionaries Introduction
>>> things = set()
>>> things.add('a string')
>>> print things
set(['a string'])
>>> things.add([1, 2, 3])
TypeError: unhashable type: 'list'
Let's try an experiment
What's wrong?
And what does the error message mean?
![Page 7: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/7.jpg)
Sets and Dictionaries Introduction
How are sets stored in a computer's memory?
![Page 8: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/8.jpg)
Sets and Dictionaries Introduction
How are sets stored in a computer's memory?
Could use a list
![Page 9: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/9.jpg)
Sets and Dictionaries Introduction
def set_create():
return []
How are sets stored in a computer's memory?
Could use a list
![Page 10: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/10.jpg)
Sets and Dictionaries Introduction
def set_create():
return []
def set_in(set_list, item):
for thing in set_list:
if thing == item:
return True
return False
How are sets stored in a computer's memory?
Could use a list
![Page 11: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/11.jpg)
Sets and Dictionaries Introduction
def set_add(set_list, item):
for thing in set_list:
if thing == item:
return
set.append(item)
![Page 12: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/12.jpg)
Sets and Dictionaries Introduction
def set_add(set_list, item):
for thing in set_list:
if thing == item:
return
set.append(item)
How efficient is this?
![Page 13: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/13.jpg)
Sets and Dictionaries Introduction
def set_add(set_list, item):
for thing in set_list:
if thing == item:
return
set.append(item)
How efficient is this?
With N items in the set, in and add take 1 to N steps
![Page 14: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/14.jpg)
Sets and Dictionaries Introduction
def set_add(set_list, item):
for thing in set_list:
if thing == item:
return
set.append(item)
How efficient is this?
With N items in the set, in and add take 1 to N steps
"Average" is N/2
![Page 15: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/15.jpg)
Sets and Dictionaries Introduction
def set_add(set_list, item):
for thing in set_list:
if thing == item:
return
set.append(item)
How efficient is this?
With N items in the set, in and add take 1 to N steps
"Average" is N/2
It's possible to do much better
![Page 16: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/16.jpg)
Sets and Dictionaries Introduction
def set_add(set_list, item):
for thing in set_list:
if thing == item:
return
set.append(item)
How efficient is this?
With N items in the set, in and add take 1 to N steps
"Average" is N/2
It's possible to do much better
But the solution puts some constraints on programs
![Page 17: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/17.jpg)
Sets and Dictionaries Introduction
Start simple: how do we store a set of integers?
![Page 18: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/18.jpg)
Sets and Dictionaries Introduction
Start simple: how do we store a set of integers?
If the range of possible values is small and fixed,
use a list of Boolean flags ("present" or "absent")
![Page 19: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/19.jpg)
Sets and Dictionaries Introduction
Start simple: how do we store a set of integers?
If the range of possible values is small and fixed,
use a list of Boolean flags ("present" or "absent")
TrueFalseTrue
0
1
2
== {0, 2}
![Page 20: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/20.jpg)
Sets and Dictionaries Introduction
Start simple: how do we store a set of integers?
If the range of possible values is small and fixed,
use a list of Boolean flags ("present" or "absent")
But what if the range of values is large, or can
change over time?
TrueFalseTrue
0
1
2
== {0, 2}
![Page 21: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/21.jpg)
Sets and Dictionaries Introduction
Use a fixed-size hash table of length L
![Page 22: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/22.jpg)
Sets and Dictionaries Introduction
Use a fixed-size hash table of length L
Store the integer I at location I % L
![Page 23: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/23.jpg)
Sets and Dictionaries Introduction
Use a fixed-size hash table of length L
Store the integer I at location I % L
'%' is the remainder operator
![Page 24: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/24.jpg)
Sets and Dictionaries Introduction
Use a fixed-size hash table of length L
Store the integer I at location I % L
'%' is the remainder operator
0
1
2
3
4
1625
101
3378
=={3378, 1625, 101}
![Page 25: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/25.jpg)
Sets and Dictionaries Introduction
Time to insert or look up is constant (!)
![Page 26: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/26.jpg)
Sets and Dictionaries Introduction
Time to insert or look up is constant (!)
But what do we do when there's a collision?
![Page 27: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/27.jpg)
Sets and Dictionaries Introduction
Time to insert or look up is constant(!)
But what do we do when there's a collision?
0
1
2
3
4
1625
101
3378+ 206
![Page 28: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/28.jpg)
Sets and Dictionaries Introduction
Option #1: store it in the next empty slot
0
1
2
3
4
1625
101
3378
206
![Page 29: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/29.jpg)
Sets and Dictionaries Introduction
Option #2: chain values together
0
1
2
3
4
1625
101
3378
206
![Page 30: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/30.jpg)
Sets and Dictionaries Introduction
Either works well until the table is about 3/4 full
![Page 31: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/31.jpg)
Sets and Dictionaries Introduction
Either works well until the table is about 3/4 full
Then average time to look up/insert rises rapidly
![Page 32: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/32.jpg)
Sets and Dictionaries Introduction
Either works well until the table is about 3/4 full
Then average time to look up/insert rises rapidly
So enlarge the table
![Page 33: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/33.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
1625
101
3378
Either works well until the table is about 3/4 full
Then average time to look up/insert rises rapidly
So enlarge the table
![Page 34: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/34.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
1625
101
3378+ 206
Either works well until the table is about 3/4 full
Then average time to look up/insert rises rapidly
So enlarge the table
![Page 35: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/35.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
1625
101
3378+ 206
0
1
2
3
4
1625
101
3378
5
6
7
8
=>
Either works well until the table is about 3/4 full
Then average time to look up/insert rises rapidly
So enlarge the table
![Page 36: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/36.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
1625
101
3378+ 206
0
1
2
3
4
1625
101
3378
5
6
7
8
=>
206
Either works well until the table is about 3/4 full
Then average time to look up/insert rises rapidly
So enlarge the table
![Page 37: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/37.jpg)
Sets and Dictionaries Introduction
How do we store strings?
![Page 38: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/38.jpg)
Sets and Dictionaries Introduction
How do we store strings?
Use a hash function to generate an integer index
based on the characters in the string
![Page 39: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/39.jpg)
Sets and Dictionaries Introduction
"zebra"
![Page 40: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/40.jpg)
Sets and Dictionaries Introduction
"zebra"
z
e
b
r
a
==
![Page 41: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/41.jpg)
Sets and Dictionaries Introduction
"zebra"
z
e
b
r
a
==
122
101
98
114
97
==
![Page 42: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/42.jpg)
Sets and Dictionaries Introduction
"zebra"
z
e
b
r
a
==
122
101
98
114
97
==
532
![Page 43: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/43.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
"zebra"
"zebra"
z
e
b
r
a
==
122
101
98
114
97
==
532 % 5
![Page 44: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/44.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
"zebra"
"zebra"
z
e
b
r
a
==
122
101
98
114
97
==
532 % 5
If we can define a hash function for something,
we can store it in a set
![Page 45: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/45.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
"zebra"
"zebra"
z
e
b
r
a
==
122
101
98
114
97
==
532 % 5
If we can define a hash function for something,
we can store it in a set
So long as nothing changes behind our back
![Page 46: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/46.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
z
e
b
r
a
This is what the previous example really looks like
in memory
![Page 47: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/47.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
z
e
b
r
a
This is what the previous example really looks like
in memory
Let's take a look at what happens if we use a list
![Page 48: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/48.jpg)
Sets and Dictionaries Introduction
['z','e','b','r','a']
![Page 49: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/49.jpg)
Sets and Dictionaries Introduction
['z','e','b','r','a']
0
1
2
3
4
'z'
'e'
'r'
== 'b'
'a'
![Page 50: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/50.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
['z','e','b','r','a']
0
1
2
3
4
'z'
'e'
'r'
== 'b'
'a'
523 % 5
![Page 51: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/51.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
['z','e','b','r','a']
0
1
2
3
4
'z'
'e'
'r'
== 'b'
'a'
523 % 5
![Page 52: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/52.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
0
1
2
3
4
'z'
'e'
'r'
'b'
'a'
This is what's actually in memory
![Page 53: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/53.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
0
1
2
3
4
'z'
'e'
'r'
'b'
'a'
This is what's actually in memory
What happens if we change the values in the list?
![Page 54: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/54.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
0
1
2
3
4
'z'
'e'
'r'
'b'
'a'
![Page 55: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/55.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
0
1
2
3
4
's'
'e'
'r'
'b'
'a'
![Page 56: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/56.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
0
1
2
3
4
's'
'e'
'r'
'b'
'a'
523 % 5
![Page 57: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/57.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
0
1
2
3
4
's'
'e'
'r'
'b'
'a'
523 % 5
The list is stored in
the wrong place!
![Page 58: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/58.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
0
1
2
3
4
's'
'e'
'r'
'b'
'a'
523 % 5
The list is stored in
the wrong place!
['s','e','b','r','a'] in S
![Page 59: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/59.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
0
1
2
3
4
's'
'e'
'r'
'b'
'a'
523 % 5
The list is stored in
the wrong place!
['s','e','b','r','a'] in S
looks at index 0 and says False
![Page 60: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/60.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
0
1
2
3
4
's'
'e'
'r'
'b'
'a'
523 % 5
The list is stored in
the wrong place!
['s','e','b','r','a'] in S
looks at index 0 and says False
['z','e','b','r','a'] in S
![Page 61: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/61.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
0
1
2
3
4
's'
'e'
'r'
'b'
'a'
523 % 5
The list is stored in
the wrong place!
['s','e','b','r','a'] in S
looks at index 0 and says False
['z','e','b','r','a'] in S
looks at index 2 and says True
![Page 62: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/62.jpg)
Sets and Dictionaries Introduction
0
1
2
3
4
0
1
2
3
4
's'
'e'
'r'
'b'
'a'
523 % 5
The list is stored in
the wrong place!
['s','e','b','r','a'] in S
looks at index 0 and says False
['z','e','b','r','a'] in S
looks at index 2 and says True (or blows up)
![Page 63: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/63.jpg)
Sets and Dictionaries Introduction
This problem arises with any mutable structure
![Page 64: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/64.jpg)
Sets and Dictionaries Introduction
This problem arises with any mutable structure
Option #1: keep track of the sets an object is in,
and update pointers every time the object changes
![Page 65: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/65.jpg)
Sets and Dictionaries Introduction
This problem arises with any mutable structure
Option #1: keep track of the sets an object is in,
and update pointers every time the object changes
Very expensive
![Page 66: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/66.jpg)
Sets and Dictionaries Introduction
This problem arises with any mutable structure
Option #1: keep track of the sets an object is in,
and update pointers every time the object changes
Very expensive
Option #2: allow it, and blame the programmer
![Page 67: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/67.jpg)
Sets and Dictionaries Introduction
This problem arises with any mutable structure
Option #1: keep track of the sets an object is in,
and update pointers every time the object changes
Very expensive
Option #2: allow it, and blame the programmer
Very expensive when it goes wrong
![Page 68: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/68.jpg)
Sets and Dictionaries Introduction
This problem arises with any mutable structure
Option #1: keep track of the sets an object is in,
and update pointers every time the object changes
Very expensive
Option #2: allow it, and blame the programmer
Very expensive when it goes wrong
Option #3: only permit immutable objects in sets
![Page 69: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/69.jpg)
Sets and Dictionaries Introduction
This problem arises with any mutable structure
Option #1: keep track of the sets an object is in,
and update pointers every time the object changes
Very expensive
Option #2: allow it, and blame the programmer
Very expensive when it goes wrong
Option #3: only permit immutable objects in sets
(If an object can't change, neither can its hash value)
![Page 70: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/70.jpg)
Sets and Dictionaries Introduction
This problem arises with any mutable structure
Option #1: keep track of the sets an object is in,
and update pointers every time the object changes
Very expensive
Option #2: allow it, and blame the programmer
Very expensive when it goes wrong
Option #3: only permit immutable objects in sets
(If an object can't change, neither can its hash value)
Slightly restrictive, but never disastrous
![Page 71: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/71.jpg)
Sets and Dictionaries Introduction
So how do we store values that naturally have
several parts, like first name and last name?
![Page 72: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/72.jpg)
Sets and Dictionaries Introduction
So how do we store values that naturally have
several parts, like first name and last name?
Option #1: concatenate them
![Page 73: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/73.jpg)
Sets and Dictionaries Introduction
So how do we store values that naturally have
several parts, like first name and last name?
Option #1: concatenate them
'Charles' and 'Darwin' stored as 'Charles|Darwin'
![Page 74: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/74.jpg)
Sets and Dictionaries Introduction
So how do we store values that naturally have
several parts, like first name and last name?
Option #1: concatenate them
'Charles' and 'Darwin' stored as 'Charles|Darwin'
(Can't use space to join 'Paul Antoine' and 'St. Cyr')
![Page 75: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/75.jpg)
Sets and Dictionaries Introduction
So how do we store values that naturally have
several parts, like first name and last name?
Option #1: concatenate them
'Charles' and 'Darwin' stored as 'Charles|Darwin'
(Can't use space to join 'Paul Antoine' and 'St. Cyr')
But data always changes...
![Page 76: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/76.jpg)
Sets and Dictionaries Introduction
So how do we store values that naturally have
several parts, like first name and last name?
Option #1: concatenate them
'Charles' and 'Darwin' stored as 'Charles|Darwin'
(Can't use space to join 'Paul Antoine' and 'St. Cyr')
But data always changes...
Code has to be littered with joins and splits
![Page 77: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/77.jpg)
Sets and Dictionaries Introduction
Option #2 (in Python): use a tuple
![Page 78: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/78.jpg)
Sets and Dictionaries Introduction
Option #2 (in Python): use a tuple
An immutable list
![Page 79: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/79.jpg)
Sets and Dictionaries Introduction
Option #2 (in Python): use a tuple
An immutable list
Contents cannot be changed after tuple is created
![Page 80: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/80.jpg)
Sets and Dictionaries Introduction
>>> full_name = ('Charles', 'Darwin')
![Page 81: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/81.jpg)
Sets and Dictionaries Introduction
>>> full_name = ('Charles', 'Darwin')
Use '()' instead of '[]'
![Page 82: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/82.jpg)
Sets and Dictionaries Introduction
>>> full_name = ('Charles', 'Darwin')
>>> full_name[0]
Charles
![Page 83: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/83.jpg)
Sets and Dictionaries Introduction
>>> full_name = ('Charles', 'Darwin')
>>> full_name[0]
Charles
>>> full_name[0] = 'Erasmus'
TypeError: 'tuple' object does not support item
assignment
![Page 84: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/84.jpg)
Sets and Dictionaries Introduction
>>> full_name = ('Charles', 'Darwin)
>>> full_name[0]
Charles
>>> full_name[0] = 'Erasmus'
TypeError: 'tuple' object does not support item
assignment
>>> names = set()
>>> names.add(full_name)
>>> names
set([('Charles', 'Darwin')])
![Page 85: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/85.jpg)
Sets and Dictionaries Introduction
This episode has been about the science of computer
science
![Page 86: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/86.jpg)
Sets and Dictionaries Introduction
This episode has been about the science of computer
science
- Designs for hash tables
![Page 87: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/87.jpg)
Sets and Dictionaries Introduction
This episode has been about the science of computer
science
- Designs for hash tables
- Mutability, usability, and performance
![Page 88: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/88.jpg)
Sets and Dictionaries Introduction
This episode has been about the science of computer
science
- Designs for hash tables
- Mutability, usability, and performance
It's a lot to digest in one go...
![Page 89: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/89.jpg)
Sets and Dictionaries Introduction
This episode has been about the science of computer
science
- Designs for hash tables
- Mutability, usability, and performance
It's a lot to digest in one go...
...but sometimes you need a little theory to make
sense of practice
![Page 90: Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See](https://reader035.vdocuments.us/reader035/viewer/2022081603/56649f3d5503460f94c5dd61/html5/thumbnails/90.jpg)
July 2010
created by
Greg Wilson
Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.