Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set

Similar presentations


Presentation on theme: "Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set"— Presentation transcript:

1 Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
We’ll do more with dictionaries later

2 Lab notes Interactive run – hard to tell when program’s output begins and ends – what can you do? Review Battleship How would you change to handle real game?

3 Sorting Often we want to sort data in a list, but our list does not contain atoms like single numbers. Need to tell Python how to compare elements Analogous to comparators in Java. Steps: Create function, taking 2 arbitrary elements from your list. Return positive / negative / zero. Call: list.sort(comparatorFunction) See example handout (compare.py)

4 Tuple Similar to list: use ( ) instead of [ ]
Good for identifying a point in some space, like an ordered pair, triple Often an anonymous object Immutable – not meant to be updated, just throw away We just have count & index functions Syntax is straightforward: multiple assignment Can re-use variables later (a, b, c) = (10, 8, 2)

5 Dictionary A nice array
Index can be anything that Python can easily evaluate, such as a single int, float or string. Typical procedure: Initialize as { } Add element by assignment, e.g. d[“USA”] = 308 Can traverse dictionary elegantly for i in d: print i, d[ i ]

6 Notes In a dictionary, like a set, the order of the data is irrelevant. The “key” is already an index. Example: { “Finland”: 5, “India”: 1150, “USA”, 308, “France” : 61 } Don’t rely on Finland being at the “beginning” of the dictionary. The value 5 is obtained by d[“Finland”], not d[0] ! Python can quickly find your data in a dictionary 

7 Illustration “Kevin” in d  returns True “James” in d  returns False
d[“Tina”] = 3 d[“Kevin”] = 2 “Kevin” in d  returns True “James” in d  returns False d [“James”] gives a KeyError, so when in doubt, check to see that key actually exists!

8 Applications Good for sparse array: only store the values you actually use. Here, the value at each key can be a list: f [1920] = [90, 230] f [1920][0] = 90 1920 1951 1960 1986 2000 2010 90 150 650 6656 18768 37728 230 700 750 3176 5144 9572

9 Applications (2) Excellent for maintaining data
E.g. Stock portfolio: reading a list of buy/sell transactions. At any point in time we may want to know total holdings Can remove an element from a dictionary using del: del portfolio[“IBM”] del enemy[“Soviet Union”]

10 Set Essentially, a set is a list in which the elements do not repeat. (See chapter 7) E.g. Useful when you need a lot of boolean values Can convert a list to a set by using set( ). s = set([1, 3, 5, 7, 9]) If you call set( ) on a string, you get a set of its characters! Operations in not in & | ^ Note that you can’t use ~


Download ppt "Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set"

Similar presentations


Ads by Google