Changes

Jump to: navigation, search

OPS435 Python Lab 4

42 bytes added, 17:07, 14 June 2017
PART 2 - Sets
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>s2s3
s2 ^ s3 # returns a set containing all values that both sets DO NOT share
s2.symmetric_difference(s3) # same as s2 ^ s3
</source>:#These powerful features can be useful and efficient, try applying them to lists. Lists <u>cannot </u> perform these operations on them, instead we have to convert the lists into sets. Perform the comparision then convert the list back to a set. There are two problems with doing this: First, sets are unordered so if the list order is important this will cause problems and remove order, second, sets cannot contain duplicate values, if the list contains any duplicate values they will be deleted. However, if the list does not have any of the above requirements this is a great solution to some problems.<source>
l2 = [1, 2, 3, 4, 5]
l3 = [4, 5, 6, 7, 8]
13,420
edits

Navigation menu