Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 4

768 bytes removed, 20:52, 11 February 2018
PART 2 - Sets
::*Sets '''cannot contain duplicate values'''
:Since new duplicate entries will be automatically removed when using sets, they are very useful for performing tasks such as '''comparisons''': '''finding similarities or differences in multiple sets'''. Also, sets are considered to be fast!
:'''Perform the Following Steps:''':#Within your ipython3 shell, create a few Create some sets to work with by issuing the followingin a temporary Python file:<source lang="python">
s1 = {'Prime', 'Ix', 'Secundus', 'Caladan'}
s2 = {1, 2, 3, 4, 5}
s3 = {4, 5, 6, 7, 8}
</source>Note: '''Sets are defined by using braces { }''' as opposed to tuples that which use parenthesis ( ), or lists that which use square brackets [ ]'''<br/><br/>:#Try to issue the following to access a set through the index.:<sourcelang="python">print(s1[0])</source>This should have created caused an '''error''', this is not how to . You cannot access data inside a set this way because they the elements inside are '''un-orderedunordered'''. Instead, you should use the '''in''' method (used in the previous section) to check to see if whether a value is contained within in the set.<br><br>:#To demonstrate, issue the following:<sourcelang="python">print('Ix' in s1)print('Geidi' in s1)
</source><br>'''Sets can be combined''', but it is important to note that any '''duplicate values (shared among sets) will be deleted'''.<br><br>
:#Issue Print the following, contents of the sets and note the items (and values) that are common to the following sets:<source lang="python">print(s2)print(s3)
</source>
:#Now, issue the following to return This is how you get a set containing only UNIQUE values (no duplicates) from both sets:<source>print(s2 | s3 ) # returns a set containing all values from both setsprint(s2.union(s3)) # same as s2 | s3</source>Notice that both methods above provides have the same result, but the first method requires less keystrokeswhich one you choose depends purely on your style.<br><br>Instead of combining sets, we can display '''values that are common to both sets'''. This is known in mathematical terms as an '''intersection''' between the lists.<br><br>:#To demonstrate intersection between sets s2 and s3, issue the following:<source lang="python">print(s2 & s3 ) # returns a set containing all values that s2 and s3 shareprint(s2.intersection(s3)) # same as s2 & s3
</source>
:#Sets can also have their values compared against other sets. First find out what items are in '''s2''' but not in '''s3'''. This is also called a '''difference'''. But notice that it only shows values that '''s2''' contains, specifically values that '''s3''' doesn't have. So this isn't really the <u>true</u> difference between the sets.:<source lang="python">print(s2)print(s3)print(s2 - s3 ) # returns a set containing all values in s2 that are not found in s3print(s2.difference(s3)) # same as s2 - s3
</source>
:#In order to see <u>every</u> difference between both sets, you need to find the '''symmetric difference'''. This will return a set that shows all numbers that both sets do not share together.<br><br>:#To demonstrate, issue the following:<source lang="python">print(s2 ^ s3 ) # returns a set containing all values that both sets DO NOT shareprint(s2.symmetric_difference(s3)) # same as s2 ^ s3</source>Note: the '''set()''' function can make convert lists into sets, and the '''list()''' function can make convert sets into lists<br><br>These powerful features . The operations in this section can only be useful and efficient. Unfortunately, lists <u>cannot</u> perform these operations, unless we have applied to convert the lists into sets. In order to that, so if you should first need to perform a comparisonunion, 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'''intersection, if the list contains any duplicate values they will be deleted. :::Howeveror difference between lists, if the list does not have any of the above requirements this is a great solution you need to convert them to some problemssets first.  :::10. To demonstrate, issue the followingFor example:<source lang="python">
l2 = [1, 2, 3, 4, 5]
l3 = [4, 5, 6, 7, 8]
new_list temporary_set = list(set(l2).intersection(set(l3))new_list = list(temporary_set) # '''set()''' can make lists into sets. '''list()''' can make sets into lists.print(new_list)
</source>
def join_sets(s1, s2):
# join_sets will return a set that has contains every value from both s1 and s2 inside it
def match_sets(s1, s2):
::::'''Sample Run 1:'''<source>
run ./lab4a.py
set1: {1, 2, 3, 4, 5, 6, 7, 8, 9}
set2: {5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
set1 = {1,2,3,4,5}
set2 = {2,1,0,-1,-2}
print(lab4a.join_sets(set1,set2))# Will output {-2, -1, 0, 1, 2, 3, 4, 5}print(lab4a.match_sets(set1,set2))# Will output {1, 2}print(lab4a.diff_sets(set1,set2))# Will output {-2, -1, 0, 3, 4, 5}
</source>
<ol><li value='3' style="margin-left:::3. Exit the ipython3 shell, download 40px;">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
ls CheckLab4.py || wget https://raw.githubusercontent.com/Seneca-CDOT/ops435/master/LabCheckScripts/CheckLab4.py
python3 ./CheckLab4.py -f -v lab4a
</source></li><li style="margin-left:::4. 40px;">Before proceeding, make certain that you identify any and all errors in lab4a.py. When the checking script tells you everything is OK before proceeding - proceed to the next step.</li></ol>
=== Create a Python Script Demonstrating Comparing Lists ===