Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2019 CISC101 5/26/2019 CISC101 Reminders

Similar presentations


Presentation on theme: "Winter 2019 CISC101 5/26/2019 CISC101 Reminders"— Presentation transcript:

1 Winter 2019 CISC101 5/26/2019 CISC101 Reminders Assignment 4 due this Friday. Use lists, sets, dictionaries and design and write functions to solve some data mining exercises. Quiz 3 grading nears completion… Quiz 4 next week! Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Today Dictionaries. Lists of Lists. Passing by Reference.
Next Big Topic – Algorithms. Winter 2019 CISC101 - Prof. McLeod

3 Dictionaries Or a “dict”. Another built-in collection type in Python.
Mutable collection (like lists). A “mapped” data structure – not index based. Uses a set of key : value associations. You cannot use the slice operator with a dictionary, instead you use the key name. The key name goes in a set of [ ], but this is not the slice operator in the context of dictionaries. Winter 2019 CISC101 - Prof. McLeod

4 Dictionaries, Cont. Create an empty dictionary using:
emptyDict = dict() or: emptyDict = { } Add or modify a key : value pair simply as: emptyDict['name'] = "Alan" emptyDict is now {'name': 'Alan'} Each key must be unique and hashable – strings are most common. Winter 2019 CISC101 - Prof. McLeod

5 Dictionaries, Cont. Add another key : value pair:
emptyDict['age'] = 21 emptyDict is now {'name': 'Alan', 'age': 21} key : value pairs are separated by commas. Like sets, you cannot control the order of the key value pairs in the dict – the hashing algorithm controls this. However, since you cannot use index values, the position has no meaning. Winter 2019 CISC101 - Prof. McLeod

6 A List of Dictionaries – An Example
CISC101 A List of Dictionaries – An Example >>> name1 = {'name':'Sam', 'age':18, 'SN': } >>> name2 = {'name':'Boris', 'age':21, 'SN': } >>> name3 = {'name':'Ben', 'age':19, 'SN': } >>> allNames = [name1, name2, name3] >>> allNames [{'age': 18, 'name': 'Sam', 'SN': }, {'age': 21, 'name': 'Boris', 'SN': }, {'age': 19, 'name': 'Ben', 'SN': }] >>> allNames[2]['age'] 19 >>> allNames[1]['name'] 'Boris' Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

7 Better Yet: >>> allNames = {} >>> allNames['Sam'] = {'age':18, 'SN': } >>> allNames['Boris'] = {'age':21, 'SN': } >>> allNames['Ben'] = {'age':19, 'SN': } >>> allNames {'Boris': {'age': 21, 'SN': }, 'Ben': {'age': 19, 'SN': }, 'Sam': {'age': 18, 'SN': }} >>> allNames['Boris']['age'] 21 Winter 2019 CISC101 - Prof. McLeod

8 Dictionaries, Cont. A dictionary has a method called keys() that returns an iterable list of key names: >>> name1.keys() dict_keys(['age', 'name', 'SN']) If you apply the sorted() BIF to a dictionary it returns an iterable sorted by key name to use with a for loop. See section 5.5 in the Python Tutorial. Winter 2019 CISC101 - Prof. McLeod

9 For Loop Example: Winter 2019 CISC101 - Prof. McLeod

10 Dictionary Methods Look for the Section “Mapping Types - dict” in the Python Standard Library: clear() – empties the dictionary fromkeys(keys, [values]) – returns a new dict get(key) – Obtains a key’s value, but returns None if the key is not in the dictionary. items() – returns a iterable of key, value tuples. keys() – returns an iterable of just keys pop(key) – remove key : value and return value. Winter 2019 CISC101 - Prof. McLeod

11 Dictionary Creation, Demo
See DictionaryCreation.py for demos of the dict() BIF and the fromkeys() method. Winter 2019 CISC101 - Prof. McLeod

12 Dictionary Methods, Cont.
popitem() – destructively iterates through a dictionary, returning key, value tuples. update(other) – updates existing values with values from other, which can be a dictionary or a collection of key, value tuples. values() – returns an iterable of just values. Winter 2019 CISC101 - Prof. McLeod

13 Another Example: Winter 2019 CISC101 - Prof. McLeod

14 Using Dictionaries These are particularly useful for field-based information, such as that stored in a database. It is easier to code using real field names rather than having to remember which fields are at which index locations. Winter 2019 CISC101 - Prof. McLeod

15 CISC101 Dictionary Example See DictionaryDrawingProgram.py which reads the text file: Drawing.txt. Uses a list of dictionaries, with one dictionary per line in the drawing. Note how key names are much better than indices in identifying the elements of a collection. A list of dictionaries could be a good database structure! Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

16 CISC101 Lists of Lists We know a list can hold anything, and the elements do not even have to be of all the same type: ex1 = [1, 4.0, ‘abc’, 2, ‘hello!’] So, there is no reason that an element cannot be another list (or a tuple, or a dictionary). ex2 = [4.5, [1, 2, ‘abc’], 7, ‘hello’] Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

17 Lists of Lists, Cont. For example: >>> for value in ex2:
print(value) 4.5 [1, 2, 'abc'] 7 hello Winter 2019 CISC101 - Prof. McLeod

18 Lists of Lists, Cont. How can I display the elements in the list at position 1?: >>> for value in ex2[1]: print(value) 1 2 abc Winter 2019 CISC101 - Prof. McLeod

19 Lists of Lists, Cont. Nothing new!
How do I access just the 'abc' string inside the list at position 1?: >>> ex2[1][2] = 'wxyz' >>> ex2 [4.5, [1, 2, 'wxyz'], 7, 'hello'] Winter 2019 CISC101 - Prof. McLeod

20 Lists of Lists, Cont. So, a list of lists can be used represent tabular data: ex3 = [['Sam', 18, ], ['Boris', 21, ], ['Ben', 19, ]] You could do it this way, or (better yet) use a dictionary. Sam 18 Boris 21 Ben 19 Winter 2019 CISC101 - Prof. McLeod

21 Lists of Lists, Cont. Don’t forget that list locations are index based, so you can use the slice operator. List elements stay where you put them! So, while you cannot control the order of key : value pairs within a dictionary, you can control the position of dictionaries within a list. Winter 2019 CISC101 - Prof. McLeod

22 CISC101 Passing by Reference Can a function change something in its parameter list and have the change stay (or “stick”), even when the function is complete? What kinds of arguments can be changed and how? See TestPassingByReference.py Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

23 Passing by Reference, Cont.
Observations: Immutable objects (the int, the string and the tuple) do not stay changed outside the function. (All you can do inside the function is re-assign them.) Re-assigning a mutable object, a list, does not change it. However, element by element changes (using the slice operator only!) or invoking a method belonging to a list does allow the changes to stay after the function is complete. Winter 2019 CISC101 - Prof. McLeod

24 Passing by Reference, Cont.
When you pass a list (or any object) into a function, you do not re-create the entire structure inside the function. That would be wasteful and time-consuming! Instead you just pass a reference (a memory address, or “pointer”) into the function. If the object is mutable, and its elements are changed or deleted inside the function, then that change is made to the structure created outside the function. Winter 2019 CISC101 - Prof. McLeod

25 Passing by Reference, Cont.
We will take advantage of being able to pass mutable objects (especially lists) by reference to simplify code! This also gives you a way to get more than one list out of a function without having to return a tuple of lists. But, if you are doing this maybe your function is not just doing one thing! And, returning multiple things through the parameter list can make for confusing code. Winter 2019 CISC101 - Prof. McLeod


Download ppt "Winter 2019 CISC101 5/26/2019 CISC101 Reminders"

Similar presentations


Ads by Google