introduction - v4.software-carpentry.org · the world is notmade of lists and arrays mathematicians...
TRANSCRIPT
![Page 1: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/1.jpg)
Introduction
Sets and Dictionaries
Introduction
Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.
![Page 2: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/2.jpg)
The world is notmade of lists and arrays
Sets and Dictionaries Introduction
![Page 3: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/3.jpg)
The world is notmade of lists and arrays
Mathematicians uses sets far more often
Sets and Dictionaries Introduction
![Page 4: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/4.jpg)
The world is notmade of lists and arrays
Mathematicians uses sets far more often
An unordered collection of distinct itemsAn unordered collection of distinct items
Sets and Dictionaries Introduction
![Page 5: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/5.jpg)
The world is notmade of lists and arrays
Mathematicians uses sets far more often
An unordered collection of distinct itemsAn unordered collection of distinct items
Collection: contains zero or more items
Sets and Dictionaries Introduction
![Page 6: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/6.jpg)
The world is notmade of lists and arrays
Mathematicians uses sets far more often
An unordered collection of distinct itemsAn unordered collection of distinct items
Collection: contains zero or more items
Distinct: no item appears more than once
Sets and Dictionaries Introduction
![Page 7: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/7.jpg)
The world is notmade of lists and arrays
Mathematicians uses sets far more often
An unordered collection of distinct itemsAn 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"
Sets and Dictionaries Introduction
![Page 8: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/8.jpg)
The world is notmade of lists and arrays
Mathematicians uses sets far more often
An unordered collection of distinct itemsAn 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 and Dictionaries Introduction
- This is the part people tend to trip over most
![Page 9: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/9.jpg)
Sets were added to Python after most of the
language was already defined
Sets and Dictionaries Introduction
![Page 10: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/10.jpg)
Sets were added to Python after most of the
language was already defined
- But at least they're there...- But at least they're there...
Sets and Dictionaries Introduction
![Page 11: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/11.jpg)
Sets were added to Python after most of the
language was already defined
- But at least they're there...- But at least they're there...
Python 2.6
primes = set([2, 3, 5])
Sets and Dictionaries Introduction
![Page 12: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/12.jpg)
Sets were added to Python after most of the
language was already defined
- But at least they're there...- But at least they're there...
Python 2.6 Python 2.7
primes = set([2, 3, 5]) primes = {2, 3, 5}
Sets and Dictionaries Introduction
![Page 13: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/13.jpg)
Sets were added to Python after most of the
language was already defined
- But at least they're there...- But at least they're there...
Python 2.6 Python 2.7
primes = set([2, 3, 5])
empty = set()
primes = {2, 3, 5}
empty = set()
Sets and Dictionaries Introduction
![Page 14: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/14.jpg)
Sets were added to Python after most of the
language was already defined
- But at least they're there...- But at least they're there...
Python 2.6 Python 2.7
primes = set([2, 3, 5])
empty = set()
primes = {2, 3, 5}
empty = set()
Sets and Dictionaries Introduction
Because {} was already used for something else
![Page 15: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/15.jpg)
Sets were added to Python after most of the
language was already defined
- But at least they're there...- But at least they're there...
Python 2.6 Python 3.1
primes = set([2, 3, 5])
empty = set()
primes = {2, 3, 5}
empty = set()
Sets and Dictionaries Introduction
Because {} was already used for something else
We'll use Python 2.7 notation in this lecture
![Page 16: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/16.jpg)
Naturally used to find unique items in a collection
Sets and Dictionaries Introduction
![Page 17: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/17.jpg)
# What letters are used?
letters = set()
Naturally used to find unique items in a collection
forforforfor char inininin 'ichthyosaur':
letters.add(char)
printprintprintprint letters
set(['a', 'c', 'i', 'h', 'o', 's', 'r', 'u', 't', 'y'])
Sets and Dictionaries Introduction
![Page 18: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/18.jpg)
# What letters are used?
letters = set()
Naturally used to find unique items in a collection
forforforfor char inininin 'ichthyosaur':
letters.add(char)
printprintprintprint letters
set(['a', 'c', 'i', 'h', 'o', 's', 'r', 'u', 't', 'y'])
Sets and Dictionaries Introduction
Not ordered alphabetically or by order of addition
![Page 19: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/19.jpg)
# What letters are used?
letters = set()
Naturally used to find unique items in a collection
forforforfor char inininin 'ichthyosaur':
letters.add(char)
printprintprintprint letters
set(['a', 'c', 'i', 'h', 'o', 's', 'r', 'u', 't', 'y'])
Sets and Dictionaries Introduction
Not ordered alphabetically or by order of addition
Because set elements are not ordered
![Page 20: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/20.jpg)
A much shorter way to accomplish the same goal
Sets and Dictionaries Introduction
![Page 21: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/21.jpg)
# What letters are used?
printprintprintprint set('ichthyosaur')
A much shorter way to accomplish the same goal
set(['a', 'c', 'i', 'h', 'o', 's', 'r', 'u', 't', 'y'])
Sets and Dictionaries Introduction
![Page 22: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/22.jpg)
# What letters are used?
printprintprintprint set('ichthyosaur')
A much shorter way to accomplish the same goal
set(['a', 'c', 'i', 'h', 'o', 's', 'r', 'u', 't', 'y'])
If you can loop over it, you can build a set from it
Sets and Dictionaries Introduction
![Page 23: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/23.jpg)
# What letters are used?
printprintprintprint set('ichthyosaur')
A much shorter way to accomplish the same goal
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
Sets and Dictionaries Introduction
![Page 24: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/24.jpg)
# What letters are used?
printprintprintprint set('ichthyosaur')
A much shorter way to accomplish the same goal
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
Sets and Dictionaries Introduction
set('a', 'e', 'i', 'o', 'u')
TypeError: set expected at most 1 arguments, got 5
![Page 25: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/25.jpg)
>>> ten = set(range(10)) # {0...9}
>>> lows = {0, 1, 2, 3, 4}
>>> odds = {1, 3, 5, 7, 9}
Sets and Dictionaries Introduction
![Page 26: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/26.jpg)
>>> 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])
Sets and Dictionaries Introduction
![Page 27: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/27.jpg)
>>> 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
Sets and Dictionaries Introduction
# remove all elements
>>> lows.clear()
>>> lows
set()
![Page 28: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/28.jpg)
# difference
>>> lows.difference(odds)
set([0, 2, 4])
Sets and Dictionaries Introduction
![Page 29: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/29.jpg)
# difference
>>> lows.difference(odds)
set([0, 2, 4])
# intersection
>>> lows.intersection(odds)
set([1, 3])
Sets and Dictionaries Introduction
![Page 30: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/30.jpg)
# difference
>>> lows.difference(odds)
set([0, 2, 4])
# intersection
>>> lows.intersection(odds)
set([1, 3])
# subset
>>> lows.issubset(ten)
Sets and Dictionaries Introduction
>>> lows.issubset(ten)
True
![Page 31: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/31.jpg)
# superset
>>> lows.issuperset(odds)
False
Sets and Dictionaries Introduction
![Page 32: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/32.jpg)
# superset
>>> lows.issuperset(odds)
False
# remove an element
>>> lows.remove(0)
>>> lows
set([1, 2, 3, 4])
Sets and Dictionaries Introduction
![Page 33: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/33.jpg)
# superset
>>> lows.issuperset(odds)
False
# remove an element
>>> lows.remove(0)
>>> lows
set([1, 2, 3, 4])
# symmetric difference (also called "exclusive or")
Sets and Dictionaries Introduction
# symmetric difference (also called "exclusive or")
>>> lows.symmetric_difference(odds)
set([2, 4, 5, 7, 9])
![Page 34: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/34.jpg)
# union
>>> lows.union(odds)
set([1, 2, 3, 4, 5, 7, 9])
Sets and Dictionaries Introduction
![Page 35: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/35.jpg)
# union
>>> lows.union(odds)
set([1, 2, 3, 4, 5, 7, 9])
# size
>>> len(odds)
7
Sets and Dictionaries Introduction
![Page 36: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/36.jpg)
# union
>>> lows.union(odds)
set([1, 2, 3, 4, 5, 7, 9])
# size
>>> len(odds)
7
# membership
>>> 6 in odds
Sets and Dictionaries Introduction
>>> 6 in odds
False
![Page 37: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/37.jpg)
Methods Operators
lows.difference(odds) lows - odds
lows.intersection(odds)
lows.issubset(ten)
lows.issuperset(ten)
lows & odds
lows <= ten
lows < ten
lows >= odds
lows > odds
Sets and Dictionaries Introduction
lows.symmetric_difference(odds)
lows.union(odds)
lows ^ odds
lows | odds
![Page 38: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/38.jpg)
Cannot negate a set
Sets and Dictionaries Introduction
![Page 39: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/39.jpg)
Cannot negate a set
Common in mathematics...
Sets and Dictionaries Introduction
![Page 40: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/40.jpg)
Cannot negate a set
Common in mathematics...
...but what's the negation of {1, 2} in a program?...but what's the negation of {1, 2} in a program?
Sets and Dictionaries Introduction
![Page 41: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/41.jpg)
Cannot negate a set
Common in mathematics...
...but what's the negation of {1, 2} in a program?...but what's the negation of {1, 2} in a program?
We'll solve this problem when we get to
object-oriented programming
Sets and Dictionaries Introduction
![Page 42: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/42.jpg)
Problem: cleaning up field observations
Sets and Dictionaries Introduction
![Page 43: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/43.jpg)
Problem: cleaning up field observations
One file has the names of birds our supervisor
thinks are uninteresting.thinks are uninteresting.
Sets and Dictionaries Introduction
![Page 44: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/44.jpg)
Problem: cleaning up field observations
One file has the names of birds our supervisor
thinks are uninteresting.thinks are uninteresting.
Another contains the names of all birds observed
during a three-week period in a mosquito-infested
hellhole in northern Ontario.
Sets and Dictionaries Introduction
![Page 45: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/45.jpg)
Problem: cleaning up field observations
One file has the names of birds our supervisor
thinks are uninteresting.thinks are uninteresting.
Another contains the names of all birds observed
during a three-week period in a mosquito-infested
hellhole in northern Ontario.
Copy the observation file, removing uninteresting
Sets and Dictionaries Introduction
Copy the observation file, removing uninteresting
birds along the way.
![Page 46: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/46.jpg)
'''Copy file, removing items along the way.'''
importimportimportimport sys
ifififif __name__ == '__main__':ifififif __name__ == '__main__':
to_remove = read_set(sys.argv[1])
reader = openopenopenopen(sys.argv[2], 'r')
writer = openopenopenopen(sys.argv[3], 'w')
forforforfor line inininin reader:
line = line.strip()
ifififif line not innot innot innot in to_remove:
Sets and Dictionaries Introduction
ifififif line not innot innot innot in to_remove:
writer.write(line)
reader.close()
writer.close()
![Page 47: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/47.jpg)
'''Copy file, removing items along the way.'''
importimportimportimport sys
ifififif __name__ == '__main__':ifififif __name__ == '__main__':
to_remove = read_set(sys.argv[1])
reader = openopenopenopen(sys.argv[2], 'r')
writer = openopenopenopen(sys.argv[3], 'w')
forforforfor line inininin reader:
line = line.strip()
ifififif line not innot innot innot in to_remove:
Sets and Dictionaries Introduction
ifififif line not innot innot innot in to_remove:
writer.write(line)
reader.close()
writer.close()
![Page 48: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/48.jpg)
'''Copy file, removing items along the way.'''
importimportimportimport sys
ifififif __name__ == '__main__':ifififif __name__ == '__main__':
to_remove = read_set(sys.argv[1])
reader = openopenopenopen(sys.argv[2], 'r')
writer = openopenopenopen(sys.argv[3], 'w')
forforforfor line inininin reader:
line = line.strip()
ifififif line not innot innot innot in to_remove:
Sets and Dictionaries Introduction
ifififif line not innot innot innot in to_remove:
writer.write(line)
reader.close()
writer.close()
![Page 49: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/49.jpg)
'''Copy file, removing items along the way.'''
importimportimportimport sys
ifififif __name__ == '__main__':ifififif __name__ == '__main__':
to_remove = read_set(sys.argv[1])
reader = openopenopenopen(sys.argv[2], 'r')
writer = openopenopenopen(sys.argv[3], 'w')
forforforfor line inininin reader:
line = line.strip()
ifififif line not innot innot innot in to_remove:
Sets and Dictionaries Introduction
ifififif line not innot innot innot in to_remove:
writer.write(line)
reader.close()
writer.close()
![Page 50: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/50.jpg)
'''Copy file, removing items along the way.'''
importimportimportimport sys
ifififif __name__ == '__main__':ifififif __name__ == '__main__':
to_remove = read_set(sys.argv[1])
reader = openopenopenopen(sys.argv[2], 'r')
writer = openopenopenopen(sys.argv[3], 'w')
forforforfor line inininin reader:
line = line.strip()
ifififif line not innot innot innot in to_remove:
Sets and Dictionaries Introduction
ifififif line not innot innot innot in to_remove:
writer.write(line)
reader.close()
writer.close()
![Page 51: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/51.jpg)
'''Copy file, removing items along the way.'''
importimportimportimport sys
ifififif __name__ == '__main__':ifififif __name__ == '__main__':
to_remove = read_set(sys.argv[1])
reader = openopenopenopen(sys.argv[2], 'r')
writer = openopenopenopen(sys.argv[3], 'w')
forforforfor line inininin reader:
line = line.strip()
ifififif line not innot innot innot in to_remove:
Sets and Dictionaries Introduction
ifififif line not innot innot innot in to_remove:
writer.write(line)
reader.close()
writer.close()
![Page 52: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/52.jpg)
defdefdefdef read_set(filename):
'''Read set elements from a file.'''
result = set()result = set()
reader = openopenopenopen(filename, 'r')
forforforfor line inininin result:
line = line.strip()
set.add(line)
reader.close()
returnreturnreturnreturn result
Sets and Dictionaries Introduction
returnreturnreturnreturn result
![Page 53: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/53.jpg)
defdefdefdef read_set(filename):
'''Read set elements from a file.'''
result = set()result = set()
reader = openopenopenopen(filename, 'r')
forforforfor line inininin result:
line = line.strip()
set.add(line)
reader.close()
returnreturnreturnreturn result
Sets and Dictionaries Introduction
returnreturnreturnreturn result
![Page 54: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/54.jpg)
defdefdefdef read_set(filename):
'''Read set elements from a file.'''
result = set()result = set()
reader = openopenopenopen(filename, 'r')
forforforfor line inininin result:
line = line.strip()
set.add(line)
reader.close()
returnreturnreturnreturn result
Sets and Dictionaries Introduction
returnreturnreturnreturn result
![Page 55: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/55.jpg)
defdefdefdef read_set(filename):
'''Read set elements from a file.'''
result = set()result = set()
reader = openopenopenopen(filename, 'r')
forforforfor line inininin result:
line = line.strip()
set.add(line)
reader.close()
returnreturnreturnreturn result
Sets and Dictionaries Introduction
returnreturnreturnreturn result
![Page 56: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/56.jpg)
defdefdefdef read_set(filename):
'''Read set elements from a file.'''
result = set()result = set()
reader = openopenopenopen(filename, 'r')
forforforfor line inininin result:
line = line.strip()
set.add(line)
reader.close()
returnreturnreturnreturn result
Sets and Dictionaries Introduction
returnreturnreturnreturn result
![Page 57: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/57.jpg)
result = set()
reader = openopenopenopen(filename, 'r')
to_remove = read_set(sys.argv[1])
reader = openopenopenopen(sys.argv[2], 'r')
writer = openopenopenopen(sys.argv[3], 'w')
forforforfor line inininin result:
line = line.strip()
set.add(line)
reader.close()
forforforfor line inininin reader:
line = line.strip()
ifififif line not innot innot innot in to_remove:
writer.write(line)
reader.close()
writer.close()
Sets and Dictionaries Introduction
returnreturnreturnreturn result
writer.close()
![Page 58: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/58.jpg)
removals.txt observations.txt result.txt
loonduckloonostrich
loonduckloonostrichostrich
loonostrichloon
ostrich loonduckloonostrichloon
loonduckloonloon
duck loon
Sets and Dictionaries Introduction
duckloonostrich
loonduckloonostrichloon
![Page 59: Introduction - v4.software-carpentry.org · The world is notmade of lists and arrays Mathematicians uses setsfar more often An unordered collectionof distinctitems Collection: contains](https://reader035.vdocuments.mx/reader035/viewer/2022070720/5ee0f712ad6a402d666c046b/html5/thumbnails/59.jpg)
July 2010
created by
Greg Wilson
July 2010
Copyright © Software Carpentry 2010
This work is licensed under the Creative Commons Attribution License
See http://software-carpentry.org/license.html for more information.