1
00:00:05,200 --> 00:00:07,280
For a practical use of set intersection, 

2
00:00:07,280 --> 00:00:11,200
we'll use our previous example – 
the patients in a drug trial. 

3
00:00:11,200 --> 00:00:17,085
I've created a new Python file for this 
example, which I've called trial_patients.py. 

4
00:00:18,320 --> 00:00:23,280
It's quite possible that there will be more than 
one trial being conducted, at the same time. 

5
00:00:23,280 --> 00:00:28,640
Giving different experimental drugs, to the 
same person, probably isn't a good idea. 

6
00:00:28,640 --> 00:00:31,440
In terms of the trials, it would 
be difficult to know which drug 

7
00:00:31,440 --> 00:00:36,695
was responsible for any observed effects.
It's also not good for the patient's health. 

8
00:00:36,695 --> 00:00:41,265
So we want to identify any patients who might 
have been included in more than one trial. 

9
00:00:43,520 --> 00:00:47,760
Set intersection is ideal for that – it 
can tell us who's in more than one set. 

10
00:00:47,760 --> 00:00:52,080
The intersection of 2 sets is the set of 
elements that are present in both sets. 

11
00:00:52,080 --> 00:00:55,280
That's what we want here.
Back in IntelliJ, 

12
00:00:55,280 --> 00:00:58,471
I'll create two sets, for different trials: 

13
00:01:31,440 --> 00:01:36,216
We're using patients in these sets, but the 
sets could contain anything that we're studying. 

14
00:01:36,216 --> 00:01:40,400
You might have a set of animals known to prey 
upon a certain species, and a set of animals 

15
00:01:40,400 --> 00:01:44,720
observed in an area where that species lives.
The intersection of those sets will tell 

16
00:01:44,720 --> 00:01:48,240
you what's likely to be eating the 
species that you're investigating. 

17
00:01:48,240 --> 00:01:52,174
Okay, let's check if anyone's 
been included in both trials: 

18
00:02:08,143 --> 00:02:14,392
Line 4 creates the intersection 
of our two sets. Run the program 

19
00:02:14,800 --> 00:02:18,240
and intersection produces a set 
containing Charley and Georgia. 

20
00:02:18,240 --> 00:02:22,986
You'd almost certainly spotted that already – 
there are only 4 people in each set after all. 

21
00:02:22,986 --> 00:02:27,520
We're showing how things work here, and 
giving you the confidence to rely on the results. 

22
00:02:27,520 --> 00:02:29,680
If there were hundreds, or thousands, 

23
00:02:29,680 --> 00:02:34,560
of people in each trial, you'd find 
it hard to spot people in both sets. 

24
00:02:34,560 --> 00:02:38,720
Okay, that's set intersection.
We could also have used the intersection 

25
00:02:38,720 --> 00:02:42,880
operator, which is the ampersand.
I won't use that example again, 

26
00:02:42,880 --> 00:02:48,000
because you can easily work out how to create 
the check_set intersection, using the operator. 

27
00:02:48,000 --> 00:02:53,200
What I will do, is look at something that might 
not be obvious, when you use the set operators. 

28
00:02:53,200 --> 00:02:58,320
When we looked at the union and union update 
operators, we used two sets in our examples. 

29
00:02:58,320 --> 00:03:02,160
If you have several sets, the methods 
are often more convenient – and we saw 

30
00:03:02,160 --> 00:03:07,520
a demonstration of that, when we created the 
union of all the adverse_interactions sets. 

31
00:03:07,520 --> 00:03:11,853
But that doesn't mean you can't use the 
operators, when you have several sets. 

32
00:03:11,853 --> 00:03:15,520
To demonstrate that, I'll use the 
example I mentioned, in the slides at 

33
00:03:15,520 --> 00:03:20,518
the start of this discussion of sets.
I'll comment out our existing code: 

34
00:03:22,640 --> 00:03:25,910
then I'll create the sets of 
animals that we had in the slides: 

35
00:04:06,000 --> 00:04:10,880
Ok, that's our farm and wild animals sets. 
Let's also have a set of animals that can 

36
00:04:10,880 --> 00:04:15,563
be ridden – that was the example I 
mentioned, in the earlier slides. 

37
00:04:29,360 --> 00:04:33,000
We've now got a third set, of 
some animals that can be ridden. 

38
00:04:33,000 --> 00:04:37,669
I'll use the intersection operator, to 
create the intersection of all three sets: 

39
00:04:54,160 --> 00:04:55,840
I'll run the program 

40
00:04:57,680 --> 00:05:01,520
and the intersection of these three 
sets is a set containing only horse. 

41
00:05:01,520 --> 00:05:04,867
Horse is the only animal that 
appears in all three sets. 

42
00:05:05,040 --> 00:05:08,960
I'll finish by showing the same 
thing, using the intersection method. 

43
00:05:08,960 --> 00:05:12,882
We can pass more than one set to 
the methods, as we've already seen: 

44
00:05:31,040 --> 00:05:35,840
I'll run the program:
Once again, we get a set 

45
00:05:35,840 --> 00:05:41,800
containing the only animals that appear in 
all three sets. That's horse, in this case. 

46
00:05:41,800 --> 00:05:46,800
I wont give an example of using the intersection 
update method, or the operator. Instead, 

47
00:05:46,800 --> 00:05:51,440
we'll use it in the exercise that follows.
Have a go at the exercise, and review the 

48
00:05:51,440 --> 00:05:56,160
last few videos if you need to. 
I'll see you, in the next video.

