Changes

Jump to: navigation, search

OPS435 Python Lab 4

133 bytes added, 08:26, 21 June 2017
PART 3 - Dictionaries
s2 ^ s3 # returns a set containing all values that both sets DO NOT share
s2.symmetric_difference(s3) # same as s2 ^ s3
</source>Note: the '''set()''' function can make lists into sets, and the '''list()''' function can make sets into lists<br><br>These powerful features can be useful and efficient. Unfortunately, lists <u>cannot</u> perform these operations, unless we have to convert the lists into sets. In order to that, you should first perform a comparison, then convert the list to a set.<br><br>There are two problems with performing the above-mentioned technique:
::::*Sets are '''un-ordered''' so if the list order is important this will cause problems and remove order
::::*Sets '''cannot contain duplicate values''', if the list contains any duplicate values they will be deleted.
l2 = [1, 2, 3, 4, 5]
l3 = [4, 5, 6, 7, 8]
new_list = list(set(l2).intersection(set(l3))) # '''set() ''' can make lists into sets. '''list() ''' can make sets into lists
new_list
</source>
:::4. Before proceeding, make certain that you identify any and all errors in lab4b.py. When the checking script tells you everything is OK before proceeding to the next step.
<br><br>
 
== PART 3 - Dictionaries ==
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><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>
list_of_keys = list(dict_york.keys())
:::::'''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 lists 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
:::*The script should show the exact output as the samples
13,420
edits

Navigation menu