In [1]:
set1={"pop", "rock", "soul", "hard rock", "rock", "R&B", "rock", "disco"}
set1
Out[1]:
The process of mapping is illustrated in the figure:
You can also create a set from a list as follows:
In [2]:
album_list =[ "Michael Jackson", "Thriller", 1982, "00:42:19", \
"Pop, Rock, R&B", 46.0, 65, "30-Nov-82", None, 10.0]
album_set = set(album_list)
album_set
Out[2]:
Now let us create a set of genres:
In [3]:
music_genres = set(["pop", "pop", "rock", "folk rock", "hard rock", "soul", \
"progressive rock", "soft rock", "R&B", "disco"])
music_genres
Out[3]:
In [4]:
set(['rap','house','electronic music', 'rap'])
Out[4]:
Notice that the duplicates are removed and the output is sorted.
Let us get the sum of the claimed sales:
In [7]:
A=[1,2,2,1]
B=set([1,2,2,1])
print(sum(A)==sum(B))
print(sum(A))
print(sum(B))
Now let's determine the average rating:
Let us go over Set Operations, as these can be used to change the set. Consider the set A:
In [8]:
A = set(["Thriller","Back in Black", "AC/DC"] )
A
Out[8]:
We can add an element to a set using the add() method:
In [9]:
A.add("NSYNC")
A
Out[9]:
If we add the same element twice, nothing will happen as there can be no duplicates in a set:
In [10]:
A.add("NSYNC")
A
Out[10]:
We can remove an item from a set using the remove method:
In [11]:
A.remove("NSYNC")
A
Out[11]:
We can verify if an element is in the set using the in command :
In [12]:
"AC/DC" in A
Out[12]:
Remember that with sets you can check the difference between sets, as well as the symmetric difference, intersection, and union:
Consider the following two sets:
In [13]:
album_set1 = set(["Thriller",'AC/DC', 'Back in Black'] )
album_set2 = set([ "AC/DC","Back in Black", "The Dark Side of the Moon"] )
In [14]:
album_set1, album_set2
Out[14]:
As both sets contain 'AC/DC' and 'Back in Black' we represent these common elements with the intersection of two circles.
We can find the common elements of the sets as follows:
In [15]:
album_set_3=album_set1 & album_set2
album_set_3
Out[15]:
We can find all the elements that are only contained in album_set1 using the difference method:
In [16]:
album_set1.difference(album_set2)
Out[16]:
We only consider elements in album_set1; all the elements in album_set2, including the intersection, are not included.
The difference between album_set2 and album_set1 is given by:
In [17]:
album_set2.difference(album_set1)
Out[17]:
We can also find the intersection, i.e in both album_list2 and album_list1, using the intersection command :
In [18]:
album_set1.intersection(album_set2)
Out[18]:
This corresponds to the intersection of the two circles:
The union corresponds to all the elements in both sets, which is represented by colouring both circles:
The union is given by:
In [19]:
album_set1.union(album_set2)
Out[19]:
And you can check if a set is a superset or subset of another set, respectively, like this:
In [20]:
set(album_set1).issuperset(album_set2)
Out[20]:
In [21]:
set(album_set2).issubset(album_set1)
Out[21]:
Here is an example where issubset() is issuperset() is true:
In [22]:
set({"Back in Black", "AC/DC"}).issubset(album_set1)
Out[22]:
In [23]:
album_set1.issuperset({"Back in Black", "AC/DC"})
Out[23]:
In [25]:
album_set3 = album_set1.union(album_set2)
album_set3
Out[25]:
In [26]:
album_set1.issubset(album_set3)
Out[26]:
Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.