Docsity

Dictionaries - Computer Science - Lecture Notes, Study notes of Computer Science

Partial preview of the text

Download Dictionaries - Computer Science - Lecture Notes and more Study notes Computer Science in PDF only on Docsity!

Today

  • Dictionaries (cont.)

Homework

  • HW 12 out

String split() function

For homework 12: Need to take a line of text and separate into words

The Python split() function:

line = "spam ham eggs"

words = line.split()

words

[โ€™spamโ€™, โ€™hamโ€™, โ€™eggsโ€™]

  • Creates a list of strings based on a separator
  • By default, any whitespace character (e.g., space, newline, tab)
  • Can also give a separator, e.g., line.split(โ€™,โ€™)

Dictionaries

Every key is unique in a dictionary

  • For example:

d = {โ€™aโ€™: 1, โ€™bโ€™: 2, โ€™aโ€™: 3}

d {โ€™aโ€™: 3, โ€™bโ€™: 2}

  • Note the + operator is not supported

Dictionary operations

Dictionaries support many of the same operations as lists

d = {โ€™aโ€™: 1, โ€™bโ€™: 2, โ€™cโ€™: 3}

d[โ€™aโ€™] # subscript 1

d[โ€™dโ€™] = 1 # add a key-value pair d {โ€™aโ€™: 1, โ€™bโ€™: 2, โ€™cโ€™: 3, โ€™dโ€™: 1}

d[โ€™aโ€™] = 2 # set a key value d {โ€™aโ€™: 2, โ€™bโ€™: 2, โ€™cโ€™: 3, โ€™dโ€™: 1}

d.get(โ€™aโ€™) 2

d.get(โ€™eโ€™, 0) # alt value 0

โ€™aโ€™ in d # key in dictionary True

โ€™eโ€™ in d False

โ€™eโ€™ not in d True

Dictionaries and iteration

The keys() function returns a sequence of keys

d = {โ€™aโ€™: 1, โ€™bโ€™: 1, โ€™cโ€™: 1}

for key in d.keys(): # same as: for key in d: print(key)

  • Prints: a b c

The values() function returns a sequence of values

for val in d.values(): print(val)

  • Prints: 1 1 1

The items() function returns a sequence of (key,val) tuples

for key, val in d.items(): print(key, "=", val)

  • Prints: a = 1 b = 1 c = 1

The copy() function copies a dictionary

d0 = d.copy() d

words [โ€™spamโ€™, โ€™hamโ€™, โ€™eggsโ€™] - Creates a list of strings based on a separator - By default, any whitespace character (e.g., space, newline, tab) - Can also give a separator, e.g., line.split(โ€™,โ€™) ## Dictionaries Every key is unique in a dictionary - For example: >>> d = {โ€™aโ€™: 1, โ€™bโ€™: 2, โ€™aโ€™: 3} >>> d {โ€™aโ€™: 3, โ€™bโ€™: 2} - Note the + operator is not supported ## Dictionary operations Dictionaries support many of the same operations as lists >>> d = {โ€™aโ€™: 1, โ€™bโ€™: 2, โ€™cโ€™: 3} >>> d[โ€™aโ€™] # subscript 1 >>> d[โ€™dโ€™] = 1 # add a key-value pair >>> d {โ€™aโ€™: 1, โ€™bโ€™: 2, โ€™cโ€™: 3, โ€™dโ€™: 1} >>> d[โ€™aโ€™] = 2 # set a key value >>> d {โ€™aโ€™: 2, โ€™bโ€™: 2, โ€™cโ€™: 3, โ€™dโ€™: 1} >>> d.get(โ€™aโ€™) 2 >>> d.get(โ€™eโ€™, 0) # alt value 0 >>> โ€™aโ€™ in d # key in dictionary True >>> โ€™eโ€™ in d False >>> โ€™eโ€™ not in d True ## Dictionaries and iteration The keys() function returns a sequence of keys d = {โ€™aโ€™: 1, โ€™bโ€™: 1, โ€™cโ€™: 1} for key in d.keys(): # same as: for key in d: print(key) - Prints: a b c The values() function returns a sequence of values for val in d.values(): print(val) - Prints: 1 1 1 The items() function returns a sequence of (key,val) tuples for key, val in d.items(): print(key, "=", val) - Prints: a = 1 b = 1 c = 1 The copy() function copies a dictionary >>> d0 = d.copy() >>> d {โ€™aโ€™: 1, โ€™bโ€™: 1, โ€™cโ€™: 1}

d0[โ€™aโ€™] = 2

d {โ€™aโ€™: 2, โ€™bโ€™: 1, โ€™cโ€™: 1}

d {โ€™aโ€™: 1, โ€™bโ€™: 1, โ€™cโ€™: 1}

The len() function returns the length of a dictionary

len(d) 3