In [1]:
%matplotlib inline
In [2]:
import numpy as np
import matplotlib.pyplot as pl
A dictionary is a way to store data. It uses a key (usually text like a word or a number) that points to a value. Think of a real dictionary. You look up a word (the key) in the dictionary to find the definition (the value).
The keys can be int, float, str, or tuple.
Dictionaries can be nested like lists.
With a list, we must remember the order of the items in the list.
Let's look at average temperatures in European cities as an example.
In [3]:
cityList = ['Oslo', 'London', 'Paris']
tempList = [13, 15.4, 17.5]
In this case, if we were interested in picking out the average temperature in London, the lists are small enough that we can determine the corresponding index visually.
Q. How could we get to the average temperature of London?
In [5]:
print(cityList[1])
print(tempList[1])
In [7]:
# We could have sublists consisting of cities and temperatures
# and search for London, then its corresponding temperature.
# Alternatively, we could index the cities list:
i = cityList.index('London')
print("Index:", i)
print("Value:", tempList[i])
In [8]:
tempDict = {'Oslo': 13, 'London': 15.4, 'Paris': 17.5}
tempDict
Out[8]:
In [9]:
temp2Dict = dict(Oslo=13, London=15.4, Paris=17.5)
temp2Dict
Out[9]:
In [10]:
print("London:", tempDict['London'])
In [12]:
tempDict['Madrid'] = 26.0
tempDict
Out[12]:
In [13]:
print(cityList)
print(tempList)
In [14]:
cityList.append("Madrid")
tempList.append(26.0)
print(cityList)
print(tempList)
In [16]:
for city in sorted(tempDict, reverse=True):
print('The average temperature in {} is {:g}.'.format(city, tempDict[city]))
We can check if 'Berlin' is in temps with a Boolean expression:
In [17]:
'Berlin' in tempDict
Out[17]:
or 'Oslo':
In [18]:
if 'Oslo' in tempDict:
print('Oslo:', tempDict['Oslo'])
The keys and values can be extracted:
In [20]:
print(tempDict.keys())
print(tempDict.values())
print(tempDict)
There is an important distinction between dictionaries and lists:
In dictionaries, the order of the keys is not preserved!
In [22]:
for city in tempDict:
print(city, tempDict[city])
In [23]:
# This accomplishes an alphabetical sorting:
for city in sorted(tempDict, reverse = False):
print(city, tempDict[city])
To delete a key and its associated value:
In [24]:
del tempDict['Madrid']
tempDict
Out[24]:
In [25]:
len(tempDict)
Out[25]:
Like arrays, if we assign a new variable to a dictionary, changes to values in one will be changed for both
In [27]:
temp3Dict = tempDict
tempDict
Out[27]:
In [28]:
temp3Dict['London'] = 0.0
In [29]:
tempDict
Out[29]:
The reason for this: in the above example, "temp3Dict" is a reference to "tempDict".
If this is not the desired behavior, create a copy:
In [31]:
tempCopyDict = tempDict.copy()
tempCopyDict['London'] = 1e6
tempDict['London'] = 15.4
print(tempCopyDict)
print(tempDict)
Earlier in the semester I pointed you to a blog post about this. We've now covered all concepts that appeared in that discussion:
http://nedbatchelder.com/text/names.html
Final note -- dictionary elements can be anything: ints, floats, lists, arrays, dictionaries, class instances... more in today's tutorial!
In [32]:
temp = 'One Two Three'
In [34]:
temp[2:5]
Out[34]:
To search for a substring, use the find() method, which reports the index of the start of the first appearance of the substring.
In [35]:
temp.find('Two')
Out[35]:
If the string is not found:
In [36]:
temp.find('Four')
Out[36]:
To test whether a substring occurs within a string:
In [37]:
'Four' in temp
Out[37]:
In [38]:
# An example:
if 'Four' in temp:
print('Four is in temp')
else:
print('Four not found')
This syntax is acceptable for lists, dictionaries, and strings!
The startswith and endswith methods test whether a string starts or ends with a specified substring:
In [39]:
temp
Out[39]:
In [40]:
# Q. What should this be?
temp.endswith('Three')
Out[40]:
The replace method replaces substrings:
In [43]:
print(temp)
temp2 = temp.replace('e', '3')
print(temp2)
print(temp)
String splitting: very useful for reading text files.
The split method splits strings into words separated by spaces (by default, other characters can be used too).
In [44]:
new_list = temp.split()
new_list
Out[44]:
This also works for multiline files:
In [47]:
temp3 = 'One\r\nTwo\r\nThree'
temp4 = temp3.splitlines()
print(temp3)
print(temp4)
In [48]:
text1 = "May"
text2 = "the"
text3 = "force"
text4 = "be with you"
combined = " uh ".join([text1, text2, text3, text4])
combined
Out[48]:
Manipulating text's uppercase/lowercase:
In [49]:
marvelText = "it's CLOBBERIN' time!"
print("Upper case: ", marvelText.upper())
print("Lower case: ", marvelText.lower())
print("Reverse case:", marvelText.swapcase())
print("Title case: ", marvelText.title())
print("Capital Case:", marvelText.capitalize())
In [52]:
'Pl4nck'.upper()
Out[52]:
Splitting strings up:
In [50]:
dcText = "Have you ever danced with the devil in the pale moonlight?"
print(dcText.split())
print(dcText.split("the"))
Let's do an example reading a data file string_example.dat and converting the data to numbers. First, let's see what's in the file:
In [58]:
cat string_example.dat
In [54]:
pairs = []
with open("string_example.dat", "r") as FILE:
for line in FILE:
words = line.split()
number1 = float(words[0])
number2 = float(words[1])
pair = [number1, number2]
pairs.append(pair)
pairs
This is a nice place for a dictionary!
In [57]:
data = {'x': [], 'y': []}
with open("string_example.dat", "r") as FILE:
for line in FILE:
print(line)
if line[0] == '#':
continue
words = line.split()
data['x'].append(float(words[0]))
data['y'].append(float(words[1]))
print(data)
In [59]:
pl.scatter(data['x'], data['y'])
Out[59]:
In [ ]: