logo

Python에서 두 목록을 비교하는 방법

Python은 두 목록을 비교하는 여러 가지 방법을 제공합니다. 비교는 목록의 데이터 항목이 동일한지 여부를 목록의 다른 데이터 항목과 비교하여 확인하는 프로세스입니다.

 list1 - [11, 12, 13, 14, 15] list2 - [11, 12, 13, 14, 15] Output - The lists are equal 

두 목록을 비교하는 방법은 다음과 같습니다.

  • cmp() 함수
  • set() 함수와 == 연산자
  • sort() 함수와 == 연산자
  • collection.counter() 함수
  • Reduce()와 map() 함수

cmp() 함수

그만큼 파이썬 cmp() 함수는 두 Python 객체를 비교하고 비교 결과에 따라 정수 값 -1, 0, 1을 반환합니다.

자바 불변 목록

참고 - Python 3.x 버전에서는 사용되지 않습니다.

set() 함수와 == 연산자

파이썬 세트() 기능 요소의 순서를 고려하지 않고 목록을 집합으로 조작합니다. 또한, 목록의 데이터 항목을 비교하기 위해 같음 연산자(==)를 사용합니다. 다음 예를 이해해 봅시다.

예 -

 list1 = [11, 12, 13, 14, 15] list2 = [12, 13, 11, 15, 14] a = set(list1) b = set(list2) if a == b: print('The list1 and list2 are equal') else: print('The list1 and list2 are not equal') 

산출:

 The list1 and list2 are equal 

설명:

위의 예에서는 서로 비교할 두 목록을 선언했습니다. 우리는 해당 목록을 집합으로 변환하고 == 연산자를 사용하여 각 요소를 비교했습니다. 모든 요소는 두 목록 모두에서 동일하며, 블록이 실행되면 결과가 인쇄됩니다.

== 연산자를 사용하는 sort() 메서드

파이썬 종류() 함수는 목록을 정렬하는 데 사용됩니다. 동일한 목록의 요소는 그것이 의미하는 것과 동일한 색인 위치입니다. 목록은 동일합니다.

참고 - sort() 메서드에서는 비교하기 전에 목록을 정렬하므로 목록 항목을 어떤 순서로든 전달할 수 있습니다.

다음 예를 이해해 봅시다 -

예 -

자바 교체
 import collections list1 = [10, 20, 30, 40, 50, 60] list2 = [10, 20, 30, 50, 40, 70] list3 = [50, 10, 30, 20, 60, 40] # Sorting the list list1.sort() list2.sort() list3.sort() if list1 == list2: print('The list1 and list2 are the same') else: print('The list1 and list3 are not the same') if list1 == list3: print('The list1 and list2 are not the same') else: print('The list1 and list2 are not the same') 

산출:

 The list1 and list3 are not the same The list1 and list2 are not the same 

collection.counter() 함수

컬렉션 모듈은 다음을 제공합니다. 카운터(), 목록을 효율적으로 비교합니다. 데이터를 사전 형식으로 저장하고 목록 항목의 빈도를 계산합니다.

참고 - 이 함수에서는 목록 요소의 순서가 중요하지 않습니다.

예 -

 import collections list1 = [10, 20, 30, 40, 50, 60] list2 = [10, 20, 30, 50, 40, 70] list3 = [50, 10, 30, 20, 60, 40] if collections.Counter(list1) == collections.Counter(list2): print('The lists l1 and l2 are the same') else: print('The lists l1 and l2 are not the same') if collections.Counter(list1) == collections.Counter(list3): print('The lists l1 and l3 are the same') else: print('The lists l1 and l3 are not the same') 

산출:

 The lists list1 and list2 are not the same The lists list1 and list3 are the same 

Reduce()와 map()

그만큼 지도() 함수는 함수와 Python 반복 가능 개체(목록, 튜플, 문자열 등)를 인수로 받아들이고 지도 개체를 반환합니다. 이 함수는 목록의 각 요소를 구현하고 결과로 반복자를 반환합니다.

게다가, 줄이다() 메소드는 반복 가능한 객체에 주어진 함수를 재귀적으로 구현합니다.

여기서는 두 가지 방법을 조합하여 사용하겠습니다. 그만큼 지도() 함수는 모든 반복 가능한 객체에 함수(사용자 정의 또는 람다 함수일 수 있음)를 구현합니다. 줄이다() 함수는 재귀적인 방식으로 적용되도록 처리합니다.

참고 - Reduce() 함수를 사용하려면 functool 모듈을 가져와야 합니다.

다음 예를 이해해 봅시다.

예 -

 import functools list1 = [10, 20, 30, 40, 50] list2 = [10, 20, 30, 50, 40, 60, 70] list3 = [10, 20, 30, 40, 50] if functools.reduce(lambda x, y: x and y, map(lambda a, b: a == b, list1, list2), True): print('The list1 and list2 are the same') else: print('The list1 and list2 are not the same') if functools.reduce(lambda x, y: x and y, map(lambda a, b: a == b, list1, list3), True): print('The list1 and list3 are the same') else: print('The list1 and list3 are not the same') 

산출:

 The list1 and list2 are not the same The list1 and list3 are the same 

이 섹션에서는 Python에서 두 목록을 비교하는 다양한 방법을 다루었습니다.

bash 문자열 연결