Intersection of Python sets
Two lists of numbers are given. You need to output the numbers that are included in both the first and second list. I enter the lists:
a = list(map(int, input().split()))
b = list(map(int, input().split()))
I'm trying to display a list of elements of the intersection of sets:
print(list(set(a) & set(b)))
And for
7 8 9 7 8 9
4 5 4 5
I get the output:
[]
And I would like to
[4, 5, 7, 8, 9]
0
1 answers
Your definition of "числа, которые входят как в первый, так и во второй список"
- can be perceived in two ways: as a union and as an intersection of sets.
Judging by the expected result, you need the union of (union
) sets:
In [113]: print(list(set(a) | set(b)))
[4, 5, 7, 8, 9]
Or
In [114]: print(list(set(a).union(set(b))))
[4, 5, 7, 8, 9]
5
Author: MaxU, 2018-08-13 09:58:51