Beyond Lists: Other Data Structures
description
Transcript of Beyond Lists: Other Data Structures
![Page 1: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/1.jpg)
Beyond Lists:Other Data StructuresCS303E: Elements of Computers and Programming
![Page 2: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/2.jpg)
Announcements Exam 3:
– Wednesday, May 1st Assignment 9 will be our last
project (and there was much rejoicing!)
![Page 3: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/3.jpg)
Other Data Structures Tuples Sets Dictionaries
![Page 4: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/4.jpg)
Tuples Sequential data structures
– Order matters– Lists and strings are also sequential
Very similar to lists BUT immutable– Can prevent data from being
changed Uses: employee or student
records
![Page 5: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/5.jpg)
Tuple Representation
Like lists, but with parentheses instead of square brackets
myTuple = (1,2,3)myTuple = (“a”, “b”, 3)myTuple = (1,2,1,2,3)
![Page 6: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/6.jpg)
Tuple OperationsOperation Description[] Indexing
[:] Slicing
+ Concatenation
* Repetition
len() Length
for elem in myTuple ORfor i in range(len(myTuple)) then access with []
Traverse
elem in myTuple Membership
![Page 7: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/7.jpg)
Packing and Unpacking Tuples You can also create a tuple by
packing values together:myTuple = “a”,4,”hello”
And you can unpack them:letter, num, word =
myTuple
![Page 8: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/8.jpg)
Converting Tuples Convert from tuple to list using listlist(myTuple)
Convert from list to tuple using tupletuple(myList)
![Page 9: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/9.jpg)
Sets A set is an unordered collection
with no duplicates– Similar to mathematical sets
![Page 10: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/10.jpg)
Set Representation
Create from a list:mySet = set([1,2,3,4])
myList = [1,2,3,4,5]mySet = set(myList)
Duplicate elements will be eliminated and the ordering lost
![Page 11: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/11.jpg)
Set Operations Union (|)
– Elements in one or both sets Intersection (&)
– Elements in both sets Difference (-) Symmetrical Difference (^)
– Elements in one but not both sets
![Page 12: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/12.jpg)
More Set OperationsOperation Description<setName>.add(<elem>) Add an element to
the set<setName>.clear() Remove all elements
from the set
len() Length
for elem in myTuple Traverse
elem in myTuple Membership
![Page 13: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/13.jpg)
Question:
What do the following datatypes have in common?
List, String, Tuple
A. All are immutableB. All are mutableC. All are sequentialD. All are unordered
![Page 14: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/14.jpg)
Dictionaries Unordered collection of key-value
pairs– Keys are used to find values in the
collection – Called hash tables or maps in other
languages Key must be an immutable type Keys are unique in the dictionary
![Page 15: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/15.jpg)
Dictionary Representation To represent a dictionary:{<key>:<value>,<key>:<value>}
Examples:myDictionary = {} #emptymyDictionary = {“hammer”:”tool”, “ham”:”meat”}
![Page 16: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/16.jpg)
Dictionary Operations Add to a dictionary by indexing
with a key and assigning a value:myDictionary[<key>]=<value>
Example:myDictionary = {} #emptymyDictionary[“apple”]=“fruit” #{“apple”:”fruit”}
![Page 17: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/17.jpg)
Dictionary Operations Change a value associated with a
key by simply re-doing the assignment:myDictionary[<key>]=<new_value>
Example:myDictionary[“apple”]=“veggie” #{“apple”:”veggie”}myDictionary[“apple”]=“fruit” #{“apple”:”fruit”}
![Page 18: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/18.jpg)
Dictionary Operations Remove from a dictionary by
indexing with a key and using del:del myDictionary[<key>]
Example:myDictionary = {“apple”:”fruit”} del myDictionary[“apple”] #empty
![Page 19: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/19.jpg)
Dictionary Operations Membership:
<key> in <dictionary> Traverse:
for <key> in <dictionary> List of Keys:
<dictionary>.keys() List of Values:
<dictionary>.values()
![Page 20: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/20.jpg)
More Dictionary OperationsOperation DescriptionmyDictionary.has_key(<key>) Returns True if the
dictionary contains the specified key and False otherwise
myDictionary.items() Returns a list of tuples, where each tuple contains a key and value pair
myDictionary.get(<key>,<default>)
Returns value associated with key if it exists, otherwise returns default value
myDictionary.clear() Removes all pairs from the dictionary
![Page 21: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/21.jpg)
Question: For a value to act as a key in a
dictionary, it must be:
A. A literal valueB. A stringC. ImmutableD. All of the above
![Page 22: Beyond Lists: Other Data Structures](https://reader035.fdocuments.us/reader035/viewer/2022062410/568161af550346895dd170ad/html5/thumbnails/22.jpg)
So… What types can be keys in a
dictionary?– Strings– Tuples– Numbers(!)