Lecture11-Dictionaries-3pp

Lecture11-Dictionaries-3pp - 131029 Dic$onaries CMPUT 174...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
13-10-29’ 1’ Dic$onaries CMPUT’174’ Dic$onaries Dic±onaries’are’linear’data’structures’used’to’associate’ values’to’keys’ Can’store’values’of’diFerent’types,’and’can’be’updated,’ like’lists’ But’the’way’we’reference’things’in’dic±onaries’is’diFerent!’ CMPUT174’- ’Dic±onaries’ 2’ Lists – indexed linear structures A’list’is’an’ indexed ’data’structure,’in’which’element’is’ associated’with’integer’indices’(0,’1,’2,’…)’ Recall:’ CMPUT174’- ’Dic±onaries’ 3’ Lists New empty list L = [ ] Element access L[ i ] – returns element at index i Adding new elements L.append(x) -- appends x to end L.insert(i, x) -- inserts x before index i Updating element L[ i ] = y ( index i must be in range) Removing elements L.pop( i ) -- removes and returns item at index i L.remove(x) -- removes first occurrence of x 0 1 2 3 5 4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
13-10-29’ 2’ Associa$ons with parallel lists Suppose’we’have’a’list’of’types’of’planes’ We’want’to’record’the’cruising’speed’(in’Km/h)’for’each’kind’of’ plane’ We’can’do’this’using’ parallel’lists :’one’for’the’plane’types,’the’ other’for’the’cruising’speeds’ CMPUT174’- ’Dic±onaries’ 4’ 0 1 2 3 5 4 Cost of parallel lists To’know’the’speed’of’the’E190,’we’need’to’know’its’ index In’a’real’program’you’wouldn²t’ask’the’user’for’and’index…’ Given’the’plane’name,’you²d’need’to’do’a’search’ To’Fnd’the’index’of’plane’E190,’you’need’to’check’ (poten±ally)’every’element’in’the’ planes ’list!’ CMPUT174’- ’Dic±onaries’ 5’ sequential search Dic$onaries Parallel’lists’may’work’for’simple’cases,’but’manipula±ng’ indices’is’cumbersome’(and’error-prone)’ A’dic±onary’ associates ’values’ (e.g.,’speeds)’with’other’values’ (e.g.,’plane’names)’ Without’indices!’ CMPUT174’- ’Dic±onaries’ 6’
Background image of page 2
13-10-29’ 3’ Dic$onaries Dic±onaries’store’ key : value ’pairs’ Keys’must’be’ immutable ’objects!’ Python’uses’hashing’on’the’keys’to’ store’the’dic±onary’(this’just’means’ that’they’can²t’be’something’that’ changes)’ Values’can’be’anything’(mutable’or’ immutable’objects)’ CMPUT174’- ’Dic±onaries’ 7’ Crea$ng dic$onaries As’with’lists,’we’can’create’dic±onaries’in’diferent’ways’ 1. Using’a’ mapping’ oF’ key:value’pairs’ 2. Star±ng’with’an’empty’dic±onary’ 3. Popula±ng’From’an’ iterable’(like’a’list)’with’keys’and’ values’ CMPUT174’- ’Dic±onaries’ 8’ Building a dic$onary from a mapping’ Python’calls’a’list’oF’ key:value ’pairs’a’mapping’ The’key’is’to’the’le³’oF’“:”,’the’value’is’to’the’right.’ CMPUT174’- ’Dic±onaries’ 9’ mapping
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 13

Lecture11-Dictionaries-3pp - 131029 Dic$onaries CMPUT 174...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online