logo

파이썬 | 유사한 목록 요소가 있는 목록 목록 정렬

정렬은 항상 많은 응용 프로그램에서 수행되는 핵심 작업이었으며 많은 문제의 하위 문제이기도 했습니다. 많은 변형과 기술이 논의되었으며 프로그래밍하는 동안 해당 지식이 있으면 유용할 수 있습니다. 이 문서에서는 목록이 포함된 목록의 정렬에 대해 설명합니다. 이것이 수행될 수 있는 특정 방법에 대해 논의해 보겠습니다.

방법 #1: sorted() + 목록 이해 사용

이 방법에서는 적용할 수 있는 긴 프로세스의 약어를 사용합니다. 목록이 반복되고 후속 하위 목록도 내부 목록을 정렬하는 sorted 함수를 사용하여 정렬됩니다.



센토스 vs rhel

단계 bt 단계 접근 방식:

  1. 우리는 각각 두 개의 정수를 포함하는 서로 다른 수의 목록을 포함하는 두 개의 하위 목록을 포함하는 중첩 목록 test_list를 초기화하는 것으로 시작합니다.
  2. 그런 다음 print() 함수와 str() 함수를 사용하여 목록을 인쇄 목적으로 문자열로 변환하여 원본 목록을 인쇄합니다.
  3. 우리는 sorted() 함수와 함께 list comprehension을 사용하여 각 하위 목록 내의 요소를 기반으로 test_list 내의 각 하위 목록을 정렬합니다.
  4. 결과로 정렬된 목록 목록을 res 변수에 할당합니다.
  5. 마지막으로 print() 함수와 str() 함수를 사용하여 목록을 인쇄 목적으로 문자열로 변환하여 결과로 정렬된 목록 res를 인쇄합니다.

파이썬3




# Python3 code to demonstrate> # Sorting list of lists with similar list elements> # using list comprehension + sorted()> # initializing list> test_list>=> [[[>4>,>4>], [>1>,>1>]], [[>3>,>3>], [>2>,>2>], [>5>,>5>]]]> # printing original list> print>(>'The original list : '> +> str>(test_list))> # using list comprehension + sorted()> # Sorting list of lists with similar list elements> res>=> [>sorted>(idx)>for> idx>in> test_list]> # print result> print>(>'The list after performing sort operation : '> +> str>(res))>

>

>

출력 :

원래 목록 : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]] 정렬 작업을 수행한 후 목록 : [[[1 , 1], [4, 4]], [[2, 2], [3, 3], [5, 5]]]

시간 복잡도: O(nlogn)
보조 공간: O(1)

방법 #2: map() + sorted() 사용

위 함수의 조합도 위 방법과 비슷한 작업을 수행하지만 차이점은 맵 함수를 사용하여 정렬 논리를 전체 하위 목록으로 확장하는 데 사용된다는 점입니다.

파이썬3




Bash의 for 루프

# Python3 code to demonstrate> # Sorting list of lists with similar list elements> # using map() + sorted()> # initializing list> test_list>=> [[[>4>,>4>], [>1>,>1>]], [[>3>,>3>], [>2>,>2>], [>5>,>5>]]]> # printing original list> print>(>'The original list : '> +> str>(test_list))> # using map() + sorted()> # Sorting list of lists with similar list elements> res>=> list>(>map>(>sorted>, test_list))> # print result> print>(>'The list after performing sort operation : '> +> str>(res))>

>

>

출력 :

원래 목록 : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]] 정렬 작업을 수행한 후 목록 : [[[1 , 1], [4, 4]], [[2, 2], [3, 3], [5, 5]]]

시간 복잡도: O(n*nlogn), 여기서 n은 test_list 목록의 요소 수입니다.
보조 공간: O(n), 여기서 n은 test_list 목록의 요소 수입니다.

방법 3: sort() 메서드와 함께 람다 함수를 사용합니다.

접근하다:

  1. 샘플 데이터로 test_list 목록을 초기화합니다.
  2. print() 함수를 사용하여 원본 목록을 인쇄합니다.
  3. 목록의 목록을 정렬하려면 sort() 메서드를 사용하세요. key 매개변수는 각 목록 요소를 정렬하는 람다 함수로 설정됩니다.
  4. print() 함수를 사용하여 정렬된 목록을 인쇄합니다.

다음은 위의 접근 방식을 구현한 것입니다.

파이썬3




# Python3 code to demonstrate> # Sorting list of lists with similar list elements> # using lambda function and sort()> # initializing list> test_list>=> [[[>4>,>4>], [>1>,>1>]], [[>3>,>3>], [>2>,>2>], [>5>,>5>]]]> # printing original list> print>(>'The original list : '> +> str>(test_list))> # using lambda function and sort()> # Sorting list of lists with similar list elements> test_list.sort(key>=>lambda> x:>sorted>(x))> # print result> print>(>'The list after performing sort operation : '> +> str>(test_list))>

>

>

산출

The original list : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]] The list after performing sort operation : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]]>

시간 복잡도: O(n log n), 여기서 n은 목록의 요소 수입니다.
보조 공간: O(1), 입력 목록 외에 추가 공간을 사용하지 않기 때문입니다.

방법 #4: functools.cmp_to_key() 사용

  1. functools 모듈을 가져옵니다.
  2. 두 개의 하위 목록을 인수로 사용하고 상대 순서에 따라 -1, 0 또는 1을 반환하는 비교 함수를 정의합니다.
  3. 비교 함수를 사용하여 원본 목록을 정렬하려면 functools의 sorted() 함수와 cmp_to_key() 함수를 사용하세요.

파이썬3




C의 for 루프

# Python3 code to demonstrate> # Sorting list of lists with similar list elements> # using functools.cmp_to_key()> # import functools module> import> functools> # initializing list> test_list>=> [[[>4>,>4>], [>1>,>1>]], [[>3>,>3>], [>2>,>2>], [>5>,>5>]]]> # printing original list> print>(>'The original list : '> +> str>(test_list))> # define comparison function> def> compare_lists(list1, list2):> >if> sorted>(list1) <>sorted>(list2):> >return> ->1> >elif> sorted>(list1)>>sorted>(list2):> >return> 1> >else>:> >return> 0> # using functools.cmp_to_key() and sorted()> # Sorting list of lists with similar list elements> test_list.sort(key>=>functools.cmp_to_key(compare_lists))> # print result> print>(>'The list after performing sort operation : '> +> str>(test_list))>

>

>

산출

The original list : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]] The list after performing sort operation : [[[4, 4], [1, 1]], [[3, 3], [2, 2], [5, 5]]]>

시간 복잡도: O(N * M * log(M)), 여기서 N은 하위 목록의 수이고 M은 가장 긴 하위 목록의 길이입니다.
보조공간 : O(M) , 여기서 M은 정렬 작업을 위한 가장 긴 하위 목록의 길이입니다.