Changes

Jump to: navigation, search

OPS435 Python Lab 4

8,691 bytes removed, 22:32, 11 February 2018
PART 3 - Dictionaries
== PART 3 - Dictionaries ==
::By now, you have probably been exposed to database terminology. For example, a '''database''' is a collection of ''related records''. In turn, '''records''' are a collection of ''related fields''. In order to access a record in a database, you would need to access it by '''key field(s)'''. In order words, those key field(s) are a '''key that unlocks the access to a record''' within a database. ::In Python, a '''dictionary''' is a set of key-value pairs. Dictionaries are '''unordered''', like sets, however any value can be retrieved from a dictionary if you know the key. This section will go over how to create, access, and change dictionaries, providing giving you a new powerful tool to store and manipulate data.
::'''Perform the Following Steps:'''
::#Launch the ipython3 shell:<source>ipython3</source>::#Let's begin by creating a new dictionary (for practice)in a temporary Python file:<source lang="python">
dict_york = {'Address': '70 The Pond Rd', 'City': 'Toronto', 'Postal Code': 'M3J3M6'}
</source>You should note that the syntax to define a dictionary is similar to defining sets (i.e. using '''{}''').<br>Unlike , but unlike sets, dictionaries use '''<code>key:value</code>''' pairs within the dictionary, each ''key:value'' pair in turn, are is separated by commas.<br><br>You can get help associated with your dictionary by using functions such as '''dir()''' and '''help()'''.<br><br>::#Issue All the following and note all the available functions available and how to obtain assistance with values in a dictionary objects:<source lang="python">dir(dict_york)help(dict_york)</source>All values can be viewed retrieved by using the '''dictionary.values()''' function. This particular function provides a '''list''' containing all values.<br><br>::#To demonstrate, issue the following:<source lang="python">helpprint(dict_york.values()dict_york.values()</source>All keys to access the ''key:pair'' values within a dictionary can be viewed by retrieved using the '''dictionary.keys()''' function. This function provides a '''list''' containing all keys<br><br>::#To demonstrate this, issue the following:<source lang="python">help(dict_york.keys)
dict_york.keys()
</source>Armed with this information, We can retrieve <u>individual</u> values from a dictionary by provide providing the key associated with the key:pair value<br><br>::#For example, issue the following:<source lang="python">print(dict_york['Address'])print(dict_york['Postal Code'])
</source>
::#Dictionary keys can be any '''immutable''' values (i.e. not permitted for value to be changed). Types of values include: '''strings''', '''numbers''', and '''tuples'''. Trying ::#Try adding a couple new keys key and values value to the dictionary by issuing:<source lang="python">
dict_york['Country'] = 'Canada'
print(dict_york)print(dict_york.values())print(dict_york.keys())
</source>
::#Let's add another key:value pair to our dictionary to change the province key:pair value to BC:<source lang="python">
dict_york['Province'] = 'BC'
print(dict_york)print(dict_york.values())print(dict_york.keys())</source>'''WARNING: Dictionary keys must be unique'''. Attempting to add a key that already exists in the dictionary will <u>overwrite</u> the existing value for that key!<br><br>::#To demonstrate, issue the followingFor example:<source lang="python">
dict_york['Province'] = 'ON'
print(dict_york)print(dict_york.values())print(dict_york.keys())</source>You should notice that key value for the 'Province' key has been changed back to 'ON'.<br><br>These The lists that contain the values and keys of the dictionary are not <u>real</u> python lists - they are "views of the dictionary" and therefore are <u>immutable</u>. You could change these views into usable lists by using the '''list()''' function (where the index can be used to access individual values).<br><br>::#For example, issue the following:<source lang="python">
list_of_keys = list(dict_york.keys())
print(list_of_keys[0])
</source>
::#In addition, lists can be changed into sets if we would like to perform comparisons with another set. To demonstrate, issue the following:<source lang="python">set_of_keys = set(dict_york.keys())set_of_values = set(dict_york.values())set_of_keys | set_of_values</source>::#Lists can be used with '''for loops'''. To Demonstrate, issue the following:<source lang="python">
list_of_keys = list(dict_york.keys())
for key in list_of_keys:
for value in dict_york.values()
print(value)
</source>Additional Information regarding Dictionaries:<ul><li>The values and keys can be looped over using the index as well
::#The '''range()''' function provides a list of numbers in a range.</li><li>The '''len()''' function provides a the number of items in a list.</li><li>Used together '''len()''' and '''range()''' can be used to create a list of usable indexes for a specific list</li></ul><br>Let's create a dictionary by using lists in order to store our dictionary data. First, we need to pair the keys and values of two separate lists.<br><br>
::#Issue the following:<source lang="python">
list_of_keys = list(dict_york.keys())
list_of_values = list(dict_york.values())
list_of_indexes = range(0, len(dict_york.keys()))
list_of_indexes
list_of_keys[0]
list_of_values[0]
</source>Now, let's use these '''newly-created lists''', '''len()''' &amp; '''range()''' functions with a '''for loop''' to construct our dictionary:<br><br>
::#Issue the following:<source lang="python">
list_of_keys = list(dict_york.keys())
list_of_values = list(dict_york.values())
for index in range(0, len(list_of_keys)):
print(list_of_keys[index] + '--->' + list_of_values[index])
</source>
::#Looping using indexes is not the best way to loop through a dictionary. A new dictionary could be created using this method, but this is '''not good''':<source lang="python">
list_of_keys = list(dict_york.keys())
list_of_values = list(dict_york.values())
new_dictionary = {}
for index in range(0, len(list_of_keys)):
new_dictionary[list_of_keys[index]] = list_of_values[index]
</source>
::#The above method uses a lot of memory and loops. The best method to create a dictionary from two lists is to use the zip() function:<source lang="python">
list_of_keys = list(dict_york.keys())
list_of_values = list(dict_york.values())
new_dictionary = dict(zip(list_of_keys, list_of_values))
</source>
::#Looping through the keys in a dictionary also provides a easy way to get the value for each key at the same time:<source lang="python">
for key in dict_york.keys():
print(key + '--->' + dict_york[key])
</source>
::#An alternative (possibly more efficient) method would be to cause both the key and its value to be extracted into a single (using a for loop, and using a special object):<source lang="python">
for key, value in dict_york.items():
print(key + ' | ' + value)
</source>
 
=== Create a Python Script for Managing Dictionaries ===
:'''Perform the Following Instructions'''
# Place code here - refer to function specifics in section below
def split_dictionary(dictionary):
# Place code here - refer to function specifics in section below
def shared_values(dict1, dict2):
# Place code here - refer to function specifics in section below
york = create_dictionary(list_keys, list_values)
print('York: ', york)
keys, values = split_dictionary(dict_newnham)
print('Newnham Keys: ', keys)
print('Newnham Values: ', values)
keys, values = split_dictionary(york)
print('York Keys: ', keys)
print('York Values: ', values)
common = shared_values(dict_york, dict_newnham)
print('Shared Values', common)
:::*The script should contain '''three''' functions:
:::::'''create_dictionary()'''<ol><li>'''accepts''' two lists as arguments keys and values, '''combines''' these lists together to '''create''' a dictionary</li><li>'''returns a dictionary''' that has the keys and associated values from the lists</li></ol>
:::::'''split_dictionary()'''<ol><li>'''accepts''' a single dictionary as a argument and '''splits''' the dictionary into two lists, keys and values</li><li>'''returns two lists''': The return function can return multiple lists (separated by a comma). In our case, use: '''return keys, values'''</li></ol>
:::::'''shared_values()''' <ol><li>'''accepts''' two dictionaries as arguments and '''finds''' all values that are shared between the two dictionaries<br>('''Tip:''' generate sets containing only values for each dictionary, then use a function mentioned in a previous section to store the values that are common to <u>both</u> lists)</li><li>'''returns a set''' containing '''ONLY values''' found in '''BOTH dictionaries'''</li></ol>
:::*make sure the functions have the correct number of arguments required
run lab4c.py
York: {'Country': 'Canada', 'Postal Code': 'M3J3M6', 'Address': '70 The Pond Rd', 'Province': 'ON', 'City': 'Toronto'}
Newnham Keys: ['Country', 'Postal Code', 'Address', 'Province', 'City']
Newnham Values: ['Canada', 'M2J2X5', '1750 Finch Ave E', 'ON', 'Toronto']
York Keys: ['Country', 'Postal Code', 'Address', 'Province', 'City']
York Values: ['Canada', 'M3J3M6', '70 The Pond Rd', 'ON', 'Toronto']
Shared Values {'Canada', 'ON', 'Toronto'}
</source>
::::'''Sample Run 2(with import):'''<source>
import lab4c
dict_york = {'Address': '70 The Pond Rd', 'City': 'Toronto', 'Country': 'Canada', 'Postal Code': 'M3J3M6', 'Province': 'ON'}
york = create_dictionary(list_keys, list_values)
print(york)# Will print: {'Address': '70 The Pond Rd', 'City': 'Toronto', 'Country': 'Canada', 'Postal Code': 'M3J3M6', 'Province': 'ON'} keys, values = split_dictionary(dict_newnham) keys['Country', 'Postal Code', 'Address', 'Province', 'City'] values['Canada', 'M2J2X5', '1750 Finch Ave E', 'ON', 'Toronto'] keys, values = split_dictionary(york) keys['Country', 'Postal Code', 'Address', 'Province', 'City'] values['Canada', 'M3J3M6', '70 The Pond Rd', 'ON', 'Toronto']
common = shared_values(dict_york, dict_newnham)
print(common)# Will print: {'Canada', 'ON', 'Toronto'}
</source>
:::3. Exit the ipython3 shell, download Download the checking script and check your work. Enter the following commands from the bash shell.<source lang="bash">
cd ~/ops435/lab4/
pwd #confirm that you are in the right directory
python3 ./CheckLab4.py -f -v lab4c
</source>
:::4. Before proceeding, make certain that you identify any and all errors in lab4c.py. When the checking script tells you everything is OK before proceeding proceed to the next step.<br><br> <!--== PART 4 - List Comprehension == '''SKIP THIS PART''' :We have already have had an introduction to lists. We will now explore advanced functions that use and generate lists. This is a very common practice in Python: understanding how to generate, manipulate, and apply functions to items inside a list can be incredibly useful. List comprehension is a way to build new lists from existing list and to do it faster than simply looping over lists. :'''Perform the Following Steps''' :#Let's start by creating a list and then applying some functions to each item in that list. Issue the following to create a list and then display the square for each item within that list:<source>l1 = [1, 2, 3, 4, 5]for item in l1: print(item ** 2)</source>In order to store these results (i.e. squares) for later use, you would have to create a new list and append the squares to it. This will generate a new list that contains squared values in the same positions of the first list. In this way, you are using an existing list in order to create a new (larger) list.<br><br>:#To demonstrate, issue the following:<source>l1 = [1, 2, 3, 4, 5]l2 = []for item in l1: l2.append(item ** 2)l1l2</source>Since this may be a repetitive task, it makes more sense to create a function that will append the squares to a new item within an existing list.<br><br> :#Issue the following to see how that can be performed:<source>def square(number): return number ** 2 l1 = [1, 2, 3, 4, 5]l2 = []for item in l1: l2.append(square(item)) l1l2</source>The '''map()''' function can be used to apply a function on each item in a list. This is exactly what happened in the previous example; however, using the ''map()'' function provides for better syntax, and removes the loop (including the variable that was created inside the loop). Therefore, using the ''map()'' function will make your Python script more efficient while performing the same task.<br><br>:#To demonstrate, issue the following:<source>def square(number): return number ** 2 l1 = [1,2,3,4,5]l2 = list(map(square, l1)) l1l2</source>The above ''map()'' function requires another function as well as a list. This means that before using (calling) the map() function, that other function would have to have been defined earlier in the script. This entire process can be avoided through the use of '''anonymous functions'''. This is the ability to create a simple function without defining it, and pass it on to other function calls. You will use the the '''lambda anonymous function''', which will return a function that you can use in that function immediately (i.e. without having to declare it in your script). The function takes 1 argument (called: x), and it will square that value.<br><br>:#To demonstrate, issue the following:<source>square = lambda x: x ** 2l1 = [1,2,3,4,5]l2 = list(map(square, l1)) l1l2</source>:#The above code is actually not particularly good, the whole purpose of using lambda here is we were avoiding the function definition and just quickly returning a function. However this does break down exactly what lambda does, it returns a function for use. Fix this by removing the square function and just use the return function from lambda. Now remember what map requires? map's first argument is a function, and map's second argument is a list. Here lambda will return a function and provide it as the first argument.<source>l1 = [1,2,3,4,5]l2 = list(map(lambda x: x ** 2, l1)) l1l2</source>:#Using the list comprehensions above our code will be faster and more efficient than using multiple variables and loops.-->
= INVESTIGATION 2: STRINGS =

Navigation menu