this week dictionaries - not exactly webster’s using the debugger

12
This Week Dictionaries - not exactly Webster’s • Using the debugger

Upload: kerrie-cooper

Post on 17-Dec-2015

215 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: This Week Dictionaries - not exactly Webster’s Using the debugger

This Week

• Dictionaries

- not exactly Webster’s

• Using the debugger

Page 2: This Week Dictionaries - not exactly Webster’s Using the debugger

Dictionaries

• Generalizes the concept we are used to.• Have a key : value pair• Webster’s Dictionary

key = wordvalue = definition of word

• Use the key to locate the corresponding value

• We say the key indexes into the dictionary

• Keys are unique.

Page 3: This Week Dictionaries - not exactly Webster’s Using the debugger

Dictionary ExamplesStudent grades file

key = student number, value = grade

GDP for Canada since 1900key = year, value = GDP

Growth rate of fruit flieskey = time, value = number of fruit flies

Page 4: This Week Dictionaries - not exactly Webster’s Using the debugger

Dictionaries and Python

dict_name = {key1 : value1, key2 : value2, …}

Example: – N. American fossil fuel emissions by year – Units are 1000s of metric tons of carbon

emissions = {1799 : 1, 1800 : 70, 1801 : 74,

1802 : 82, 1902 : 215630, 2002 :

1733297}

Look up entry:emissions[1902] or

emissions.get(1902)

Page 5: This Week Dictionaries - not exactly Webster’s Using the debugger

Dictionaries and Python

dict_name = {key1 : value1, key2 : value2, …}

Example: – N. American fossil fuel emissions by year – Units are 1000s of metric tons of carbon

emissions = {1799 : 1, 1800 : 70, 1801 : 74,

1802 : 82, 1902 : 215630, 2002 :

1733297}

Add entry:emissions[1950] = 734914

Page 6: This Week Dictionaries - not exactly Webster’s Using the debugger

Dictionaries and Pythondict_name = {key1 : value1, key2 : value2, …}

Example: – N. American fossil fuel emissions by year – Units are 1000s of metric tons of carbon

emissions = {1799 : 1, 1800 : 70, 1801 : 74,

1802 : 82, 1902 : 215630, 2002 :

1733297}

Delete entry:del emissions[1902]

Page 7: This Week Dictionaries - not exactly Webster’s Using the debugger

Dictionaries and Pythondict_name = {key1 : value1, key2 : value2, …}

Example: – N. American fossil fuel emissions by year – Units are 1000s of metric tons of carbon

emissions = {1799 : 1, 1800 : 70, 1801 : 74,

1802 : 82, 1902 : 215630, 2002 :

1733297}

Check for membership:1950 in emissions

Page 8: This Week Dictionaries - not exactly Webster’s Using the debugger

More Dictionary Operations

Create a list of keys emissions.keys() [1799, 1800, 1801, 1802, 1902, 2002]

Create a list of values emissions.values() [1, 70, 74, 82, 215630, 1733297]

Create a list of (key, value) pairs emissions.items() [(1799, 1), (1800, 70), (1801, 74),

(1802, 82), (1902, 215630), (2002, 1733297)]

Looping over dictionaries:for key in emissions:

print(key + “, “ + emissions[key])

Page 9: This Week Dictionaries - not exactly Webster’s Using the debugger

More Dictionary Operations

Create a list of keys emissions.keys() [1799, 1800, 1801, 1802, 1902, 2002]

Create a list of values emissions.values() [1, 70, 74, 82, 215630, 1733297]

Create a list of (key, value) pairs emissions.items() [(1799, 1), (1800, 70), (1801, 74),

(1802, 82), (1902, 215630), (2002, 1733297)]

Looping over dictionaries:for key in emissions.keys():

print(key + “, “ + emissions[key])

Page 10: This Week Dictionaries - not exactly Webster’s Using the debugger

More Dictionary Operations

Create a list of keys emissions.keys() [1799, 1800, 1801, 1802, 1902, 2002]

Create a list of values emissions.values() [1, 70, 74, 82, 215630, 1733297]

Create a list of (key, value) pairs emissions.items() [(1799, 1), (1800, 70), (1801, 74),

(1802, 82), (1902, 215630), (2002, 1733297)]

Looping over dictionaries:for value in emissions.values():

print(value)

Page 11: This Week Dictionaries - not exactly Webster’s Using the debugger

More Dictionary Operations

Create a list of keys emissions.keys() [1799, 1800, 1801, 1802, 1902, 2002]

Create a list of values emissions.values() [1, 70, 74, 82, 215630, 1733297]

Create a list of (key, value) pairs emissions.items() [(1799, 1), (1800, 70), (1801, 74),

(1802, 82), (1902, 215630), (2002, 1733297)]

Looping over dictionaries:for (key, value) in emissions.items():

print(key + ‘\t’ + value)

Page 12: This Week Dictionaries - not exactly Webster’s Using the debugger

More Dictionary Operationsemissions = {1799 : 1, 1800 : 70, 1801 : 74, 1802

: 82, 1902 : 215630, 2002 : 1733297}

emissions2 = {1850 : 5418, 1875 : 37561,

1925 : 486728, 1950 : 735036, 2004 : 1824421}

Add the contents of one dictionary to another:

emissions.update(emissions2)

emissions is: {1799 : 1, 1800 : 70, 1801 : 74, 1802 : 82, 1902

: 215630, 2002 : 1733297, 1925 : 486728,

1950 : 735036, 2004 : 1824421, 1850 : 5418,

1875 : 37561}