두 목록의 교차는 초기 목록 모두에 공통된 모든 요소를 가져와 다른 목록에 저장해야 함을 의미합니다. 이제 Python에는 목록의 교차를 수행할 수 있는 다양한 방법이 있습니다.
예:
Input : lst1 = [15, 9, 10, 56, 23, 78, 5, 4, 9] lst2 = [9, 4, 5, 36, 47, 26, 10, 45, 87] Output : [9, 10, 4, 5] Input : lst1 = [4, 9, 1, 17, 11, 26, 28, 54, 69] lst2 = [9, 9, 74, 21, 45, 11, 63, 28, 26] Output : [9, 11, 26, 28]>
방법 1:
이는 내장 함수를 사용하지 않은 가장 간단한 방법입니다.
파이썬3
힙 정렬 알고리즘
# Python program to illustrate the intersection> # of two lists in most simple way> def> intersection(lst1, lst2):> > lst3> => [value> for> value> in> lst1> if> value> in> lst2]> > return> lst3> # Driver Code> lst1> => [> 4> ,> 9> ,> 1> ,> 17> ,> 11> ,> 26> ,> 28> ,> 54> ,> 69> ]> lst2> => [> 9> ,> 9> ,> 74> ,> 21> ,> 45> ,> 11> ,> 63> ,> 28> ,> 26> ]> print> (intersection(lst1, lst2))> |
>
>
산출:
[9, 11, 26, 28]>
방법 2:
이 방법에는 set() 메소드 .
파이썬3
# Python program to illustrate the intersection> # of two lists using set() method> def> intersection(lst1, lst2):> > return> list> (> set> (lst1) &> set> (lst2))> # Driver Code> lst1> => [> 15> ,> 9> ,> 10> ,> 56> ,> 23> ,> 78> ,> 5> ,> 4> ,> 9> ]> lst2> => [> 9> ,> 4> ,> 5> ,> 36> ,> 47> ,> 26> ,> 10> ,> 45> ,> 87> ]> print> (intersection(lst1, lst2))> |
>
>
산출:
[9, 10, 4, 5]>
위 프로그램의 시간 복잡도는 O(n)입니다. 여기서 n은 lst1과 lst2 사이의 더 긴 목록의 길이입니다.
프로그램의 공간 복잡도는 O(n)입니다. 여기서 n은 lst1과 lst2 사이의 더 작은 목록의 길이입니다.
방법 3:
이 방법에서 우리는 set() 더 큰 목록 그런 다음 내장 함수를 사용하십시오. 교차점() 교차된 목록을 계산합니다. 교차점() 세트의 일류 부품입니다.
파이썬3
# Python program to illustrate the intersection> # of two lists using set() and intersection()> def> Intersection(lst1, lst2):> > return> set> (lst1).intersection(lst2)> > # Driver Code> lst1> => [> 4> ,> 9> ,> 1> ,> 17> ,> 11> ,> 26> ,> 28> ,> 28> ,> 26> ,> 66> ,> 91> ]> lst2> => [> 9> ,> 9> ,> 74> ,> 21> ,> 45> ,> 11> ,> 63> ]> print> (Intersection(lst1, lst2))> |
>
>
산출:
{9, 11}>
방법 4:
이를 이용하여 하이브리드 방식 프로그램의 복잡도는 O(n)으로 떨어집니다. 이는 다음 프로그램을 수행하는 효율적인 방법입니다.
파이썬3
# Python program to illustrate the intersection> # of two lists> def> intersection(lst1, lst2):> > # Use of hybrid method> > temp> => set> (lst2)> > lst3> => [value> for> value> in> lst1> if> value> in> temp]> > return> lst3> # Driver Code> lst1> => [> 9> ,> 9> ,> 74> ,> 21> ,> 45> ,> 11> ,> 63> ]> lst2> => [> 4> ,> 9> ,> 1> ,> 17> ,> 11> ,> 26> ,> 28> ,> 28> ,> 26> ,> 66> ,> 91> ]> print> (intersection(lst1, lst2))> |
>
>
산출:
[9, 9, 11]>
방법 5:
다른 목록 내의 하위 목록에 대해 교차가 수행되는 곳입니다. 여기서는 다음과 같은 개념을 사용했습니다. 필터().
파이썬3
# Python program to illustrate the intersection> # of two lists, sublists and use of filter()> def> intersection(lst1, lst2):> > lst3> => [> list> (> filter> (> lambda> x: x> in> lst1, sublist))> for> sublist> in> lst2]> > return> lst3> # Driver Code> lst1> => [> 1> ,> 6> ,> 7> ,> 10> ,> 13> ,> 28> ,> 32> ,> 41> ,> 58> ,> 63> ]> lst2> => [[> 13> ,> 17> ,> 18> ,> 21> ,> 32> ], [> 7> ,> 11> ,> 13> ,> 14> ,> 28> ], [> 1> ,> 5> ,> 6> ,> 8> ,> 15> ,> 16> ]]> print> (intersection(lst1, lst2))> |
>
>
일하고 있는: 필터 부분은 각 하위 목록의 항목을 가져와 소스 목록에 있는지 확인합니다. 목록 이해는 list2의 각 하위 목록에 대해 실행됩니다.
산출:
[[13, 32], [7, 13, 28], [1, 6]]>
방법 6: Reduce() 사용:
연산:
- functools 모듈에서 축소 기능을 가져옵니다.
- 두 개의 목록을 정의합니다.
- 빈 목록으로 변수 교차를 초기화합니다.
- lst1의 요소를 반복하려면 축소 함수를 사용하십시오.
- 람다 함수 내에서 현재 요소가 lst2에 있고 교차 목록에는 아직 없는지 확인합니다.
- 그렇다면 현재 요소를 교차 목록에 추가하십시오.
- 교차점 목록을 반환합니다.
- 교차점 목록을 인쇄합니다.
파이썬3
from> functools> import> reduce> lst1> => [> 15> ,> 9> ,> 10> ,> 56> ,> 23> ,> 78> ,> 5> ,> 4> ,> 9> ]> lst2> => [> 9> ,> 4> ,> 5> ,> 36> ,> 47> ,> 26> ,> 10> ,> 45> ,> 87> ]> intersection> => reduce> (> lambda> acc, x: acc> +> [x]> if> x> in> lst2> and> x> not> in> acc> else> acc, lst1, [])> print> (intersection)> #This code is contributed by Rayudu.> |
>
>산출
[9, 10, 5, 4]>
시간 복잡도: O(n^2), 여기서 n은 lst1의 길이입니다.
공간 복잡도: O(n), 여기서 n은 lst1의 길이입니다.