/
CSE 231  Lab 8 Topics to cover CSE 231  Lab 8 Topics to cover

CSE 231 Lab 8 Topics to cover - PowerPoint Presentation

caitlin
caitlin . @caitlin
Follow
342 views
Uploaded On 2021-12-08

CSE 231 Lab 8 Topics to cover - PPT Presentation

Dictionaires vs lists Creating Dictionaries Dictionaries Methods Dictionaries Visualizing Insertionordered collection of Python objects Python gt36 What is a key What is a value ID: 904536

returns key sorted dictionary key returns dictionary sorted dictionaries keys list methods items set initialize values exists pairs three

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "CSE 231 Lab 8 Topics to cover" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

CSE 231

Lab 8

Slide2

Topics to cover

Dictionaires

vs

lists

Creating Dictionaries

Dictionaries

Methods

Slide3

Dictionaries - Visualizing

Insertion-ordered collection of Python objects (Python >=3.6)

What is a key? What is a value?

Should be immutable

Any objects is allowed

Slide4

Dictionaries vs Lists

Slide5

What happens?

Given a dictionary, D, where:

D[“key”] = “value”

print(D[“key”])

“value”

Slide6

Create and update a dictionary

Are dictionaries mutable?

Creating a Dictionary

M = {}

M = dict() M = { 200: "EE", 100: "ME"}Adding/updating a value in a DictionaryM[500] = "CS"

Slide7

Patterns

Ints

:

x = 0 # initialize at zero

x += 1 # increment the counterStrings:s = ‘’ # initialize with empty strings += ch # concatenate characters Lists:L = [] # initialize with empty listL.append

(value) # append values to the list

Slide8

Patterns

Dictionaries:

D = { } # initialize with

empty dictionary

S = “aabacdbacd” # we have a string, we want to count all the charactersfor ch in S: if

ch in D: # check to see if the key exists in the dictionary D[ch

] += 1 # increment the value if it exists else: D[ch] = 1 # set the value to 1 if it doesn’t exist

Slide9

Dictionaries

Methods

Use this one most of the time:

.items() # Returns a set-like object of all the key-value pairs.Other options:

.keys() # Returns a set-like object of all the keys.

.values() # Returns a set-like pbject of all the values.

Slide10

Additional Useful Methods & functions

D =

M.get

( 200 )

returns the value if key exists, else returns NoneE = M.pop( "Mike", None )pops (and returns) the value if the key exists, returns the second argument otherwisedel M[600]

deletes if the key exist, KeyError

if it doesn'tD = dict(L) # assuming L = [('a',1),('b',2),('c',3)]D = {'a': 1, 'b': 2, 'c': 3}

Slide11

Iteration

for key, value in

M.items

(): # use most of the time

for key in M.keys():for key in M: # same as M.keys()for value in M.values():

Slide12

What happens?

Given a dictionary D,

D = {“one” : 1, “two” : 2, ”three” : 3}

for

key,value in D.items

():print(

key,value)

one 1

two 2

three 3

Slide13

What happens?

Given a dictionary D,

D = {}

Print(D[‘a’])

KeyError

Slide14

Additional useful methods and function

D = {“one” : 1, “two” : 2, ”three” : 3}

list(D)

# returns a list of all the keys used in the dictionary

['one', 'two', 'three']sorted(D)

 # returns a sorted list of all the keys in the dictionary ['one', 'three', 'two']

Slide15

Other useful methods and function

D = {“one” : 1, “two” : 2, ”three” : 3}

sorted(

D.items

()) # returns a sorted list of dictionary's (key, value) tuple pairs

[('one', 1), ('three', 3), ('two', 2)]dict

(sorted(D.items())) # in insertion order (if you want it sorted, get the list of tuple pairs and just use sorted)

{'one': 1, 'three': 3, 'two': 2}