logo

Python의 중첩 튜플

중첩 튜플은 다른 튜플 내부에 배치된 Python 튜플입니다. 다음 8개 요소로 구성된 튜플을 살펴보겠습니다.

 tuple = (12, 23, 36, 20, 51, 40, (200, 240, 100)) 

괄호 안에 세 개의 항목으로 구성된 이 마지막 요소는 다른 튜플 안에 포함되어 있으므로 중첩 튜플이라고 합니다. 인덱스 값이 있는 기본 튜플의 이름인 tuple[index]를 사용하여 중첩 튜플을 얻을 수 있으며, tuple[index-1][index-2]를 사용하여 중첩 튜플의 각 항목에 액세스할 수 있습니다.

중첩된 튜플의 예

암호

 # Python program to create a nested tuple # Creating a nested tuple of one element only employee = ((10, 'Itika', 13000),) print(employee) # Creating a multiple-value nested tuple employee = ((10, 'Itika', 13000), (24, 'Harry', 15294), (15, 'Naill', 20001), (40, 'Peter', 16395)) print(employee) 

산출:

 ((10, 'Itika', 13000),) ((10, 'Itika', 13000), (24, 'Harry', 15294), (15, 'Naill', 20001), (40, 'Peter', 16395)) 

중첩된 튜플의 일부 작업

중첩된 튜플에 필요한 두 가지 작업을 살펴보겠습니다.

튜플을 중첩 튜플에 연결

튜플을 처리할 때 별도의 레코드를 독립적인 요소로 유지하면서 중첩된 그룹으로 전환해야 하는 경우가 있습니다. 튜플은 내용을 추가하여 추가되는 경우가 많으며, 이는 결과 컨테이너를 평면화하는데 이는 일반적으로 바람직하지 않습니다. 이 문제를 해결하기 위한 몇 가지 접근 방식에 대해 이야기해 보겠습니다.

초기화 중 + 연산자 및 ',' 사용

이 기술에서는 우리가 하는 것처럼 튜플 멤버를 추가하지만, 튜플을 초기화할 때 추가하는 동안 평면화를 방지하기 위해 각 튜플 뒤에 쉼표를 추가합니다.

암호

 # Python program to concatenate tuples to make a nested tuple # initializing the tuples tup1 = (5, 4), tup2 = (1, 6), # printing the original tuples print('Tuple 1 : ' + str(tup1)) print('Tuple 2 : ' + str(tup2)) # Concatenating the two tuples to a nested tuple using the + operator nested = tup1 + tup2 # printing the result print('The nested tuple : ' + str(nested)) 

산출:

 Tuple 1 : ((5, 4),) Tuple 2 : ((1, 6),) The nested tuple : ((5, 4), (1, 6)) 

',' 연산자 사용

이 작업은 연결 중에 ',' 연산자를 적용하여 수행할 수 있습니다. 안전한 연결을 수행할 수 있습니다.

암호

 # Python program to concatenate tuples to make a nested tuple # initializing the tuples tup1 = (5, 4) tup2 = (1, 6) # printing the original tuples print('Tuple 1 : ' + str(tup1)) print('Tuple 2 : ' + str(tup2)) # Concatenating the tuples by using the ', 'operator after tuples nested = ((tup1, ) + (tup2, )) # printing result print('The nested tuple ' + str(nested)) 

산출:

 Tuple 1 : (5, 4) Tuple 2 : (1, 6) The nested tuple ((5, 4), (1, 6)) 

중첩된 튜플 정렬

sorted() 메소드를 사용하여 주어진 튜플을 정렬할 수 있습니다. 기본적으로 이 메서드는 튜플을 오름차순으로 정렬합니다. 예를 들어, print(sorted(employee))는 모든 중첩 튜플 중 0번째 멤버로 나타나는 식별 번호에 따라 'employee' 튜플을 정렬합니다. 람다 함수를 사용하면 중첩된 튜플의 첫 번째 및 두 번째 멤버인 직원 이름이나 수와 같은 중첩된 튜플의 다른 요소에 따라 튜플을 정렬할 수 있습니다. x: x[1])).

이 경우 키는 어떤 요소에 따라 튜플을 정렬해야 하는지 sorted() 함수에 알려줍니다. 람다 식:lambda x: x[1]은 정렬 시 인덱스 1 요소인 키를 고려해야 함을 의미합니다. 람다 표현식을 람다 x: x[2]로 작성하여 단어 수에 따라 튜플을 정렬할 수 있습니다.

암호

사자는 호랑이에 비해
 # Python program to sort the nested tuple using the sorted() function # Creating a nested tuple employee = ((10, 'Itika', 13000), (24, 'Harry', 15294), (15, 'Naill', 20001), (40, 'Peter', 16395)) # Sorting the tuple by default on the id print(sorted(employee)) # Sorting the tuple on id in reverse order print(sorted(employee, reverse = True)) # Sorting the tuple on name using lambda function print(sorted(employee, key = lambda x: x[1])) # Sorting the tuple on the name in reverse order print(sorted(employee, key = lambda x: x[1], reverse = True)) # Sorting the tuple on the word count print(sorted(employee, key = lambda x: x[2])) # Sorting the tuple on the word count in reverse print(sorted(employee, key = lambda x: x[2], reverse = True)) 

산출:

 [(10, 'Itika', 13000), (15, 'Naill', 20001), (24, 'Harry', 15294), (40, 'Peter', 16395)] [(40, 'Peter', 16395), (24, 'Harry', 15294), (15, 'Naill', 20001), (10, 'Itika', 13000)] [(24, 'Harry', 15294), (10, 'Itika', 13000), (15, 'Naill', 20001), (40, 'Peter', 16395)] [(40, 'Peter', 16395), (15, 'Naill', 20001), (10, 'Itika', 13000), (24, 'Harry', 15294)] [(10, 'Itika', 13000), (24, 'Harry', 15294), (40, 'Peter', 16395), (15, 'Naill', 20001)] [(15, 'Naill', 20001), (40, 'Peter', 16395), (24, 'Harry', 15294), (10, 'Itika', 13000)]