Download presentation
Presentation is loading. Please wait.
Published byAngelica Townsend Modified over 9 years ago
1
Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07
2
Computing Science 1P Tutorial 14 - Simon Gay2 If d = { "John" : 5, "Helen" : 7 }, what do we call "John" ? A key A value Don't know
3
2006/07Computing Science 1P Tutorial 14 - Simon Gay3 Question Suppose we define a dictionary by d = { "Fred":10, "Joe":15, "Anne": 23, "Clare":14 } and then run the following code: for x in d.keys(): print x In which order will the keys of the dictionary appear?
4
2006/07Computing Science 1P Tutorial 14 - Simon Gay4 What is the order? Alphabetical order of keys Numerical order of values We can't tell; it looks random The order in the definition Don't know
5
2006/07Computing Science 1P Tutorial 14 - Simon Gay5 Calculating the mode In statistics, the most frequently occurring element of a list of data is called the mode. It's a kind of average which does not require the data to have any structure at all. e.g. mode([1,2,1,1,3,4,3,2]) should return 1. We want to define a function to calculate the mode of a list of numbers. We can do this by combining two ideas we already know: - frequency analysis - finding the largest value among data
6
2006/07Computing Science 1P Tutorial 14 - Simon Gay6 Calculating the mode To calculate the mode, we need to calculate how many times each number occurs in the data. How should we do this? (Example: [1,2,1,1,3,4,3,2] ) 1.Use a list in which position i contains the number of occurrences of i Example: [ 0,3,2,2,1 ] 2.Use a dictionary in which an item with key i has a value which is the number of occurrences of i Example: { 1:3, 2:2, 3:2, 4:1 }
7
2006/07Computing Science 1P Tutorial 14 - Simon Gay7 What is your preferred data structure for this problem? The list The dictionary Don't know
8
2006/07Computing Science 1P Tutorial 14 - Simon Gay8 Discussion Think about what will happen in the following function call: mode([1,2,1,1,3,4,3,2,1000000]) Do you still like your choice of data structure? Discuss it with your neighbours.
9
2006/07Computing Science 1P Tutorial 14 - Simon Gay9 When a dictionary is better When keys are integers and the set of keys is sparse (i.e. there is a large range of possible keys but relatively few are actually used), a dictionary is better than a list. When keys are some other type (e.g. strings), so that we can't use positional lookup, a dictionary is better than a nested list because lookup is faster.
10
2006/07Computing Science 1P Tutorial 14 - Simon Gay10 Mode: counting occurrences def mode(x): d = {} for n in x: if d.has_key(n): d[n] = d[n] + 1 else: d[n] = 1 # now find the most common number
11
2006/07Computing Science 1P Tutorial 14 - Simon Gay11 What do you think of this code? It's correct: in fact, ideal It's correct but can be simplified It contains errors Don't know
12
2006/07Computing Science 1P Tutorial 14 - Simon Gay12 Mode: counting occurrences def mode(x): d = {} for n in x: d[n] = d.get(n,0)+1 # now find the most common number The code is correct but it can be simplified a little by using the technique from page 76 of the book: Exercise: complete the definition so that the most common number is returned.
13
2006/07Computing Science 1P Tutorial 14 - Simon Gay13 Using nested lists to represent a matrix A matrix (plural: matrices) is a rectangular grid of numbers. 123245312123245312 Represent it by a list of lists: [ [1,2,3], [2,4,5], [3,1,2] ] Let's think about an n n matrix: n rows and n columns, represented by a list of n lists, each of n elements.
14
2006/07Computing Science 1P Tutorial 14 - Simon Gay14 Opposite diagonal of an n n matrix Which of the following code fragments prints the "opposite" diagonal of an n n matrix m ? 1325 4102 2234 5123 n = len(m) for i in range(n): print m[i][i] n = len(m) for i in range(n): print m[i][n-i] n = len(m) for i in range(n): print m[i][n-i-1] n = len(m) for i in range(n): print m[i][n-i+1] 1 2 3 4
15
2006/07Computing Science 1P Tutorial 14 - Simon Gay15 Which piece of code? 1 2 3 4 None of them Don't know
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.