튜플 요소 복사의 기본 기술
이 단계에서는 튜플의 요소를 다른 튜플로 복사하는 기본 기술을 살펴보겠습니다. 튜플은 불변 (immutable) 이므로 복사는 실제로 동일하거나 선택된 요소를 가진 새로운 튜플을 생성하는 것을 의미합니다.
이러한 기술을 실험하기 위해 새 파일을 만들어 보겠습니다.
/home/labex/project 디렉토리에 tuple_copying_basics.py라는 새 파일을 만듭니다.
- 파일에 다음 코드를 추가합니다.
## Create a sample tuple to work with
original_tuple = (1, 2, 3, 4, 5)
print("Original tuple:", original_tuple)
## Method 1: Using the slice operator [:]
slice_copy = original_tuple[:]
print("\nMethod 1 - Using slice operator [:]")
print("Copy:", slice_copy)
print("Is it the same object?", original_tuple is slice_copy)
print("Do they have the same values?", original_tuple == slice_copy)
## Method 2: Using the tuple() constructor
constructor_copy = tuple(original_tuple)
print("\nMethod 2 - Using tuple() constructor")
print("Copy:", constructor_copy)
print("Is it the same object?", original_tuple is constructor_copy)
print("Do they have the same values?", original_tuple == slice_copy)
## Method 3: Using tuple unpacking (only for smaller tuples)
a, b, c, d, e = original_tuple
unpacking_copy = (a, b, c, d, e)
print("\nMethod 3 - Using tuple unpacking")
print("Copy:", unpacking_copy)
print("Is it the same object?", original_tuple is unpacking_copy)
print("Do they have the same values?", original_tuple == unpacking_copy)
## Method 4: Using the + operator with empty tuple
plus_copy = () + original_tuple
print("\nMethod 4 - Using + operator")
print("Copy:", plus_copy)
print("Is it the same object?", original_tuple is plus_copy)
print("Do they have the same values?", original_tuple == plus_copy)
- 파일을 저장하고 다음 명령으로 실행합니다.
python3 /home/labex/project/tuple_copying_basics.py
다음과 유사한 출력을 볼 수 있습니다.
Original tuple: (1, 2, 3, 4, 5)
Method 1 - Using slice operator [:]
Copy: (1, 2, 3, 4, 5)
Is it the same object? False
Do they have the same values? True
Method 2 - Using tuple() constructor
Copy: (1, 2, 3, 4, 5)
Is it the same object? False
Do they have the same values? True
Method 3 - Using tuple unpacking
Copy: (1, 2, 3, 4, 5)
Is it the same object? False
Do they have the same values? True
Method 4 - Using + operator
Copy: (1, 2, 3, 4, 5)
Is it the same object? False
Do they have the same values? True
선택적 복사 및 요소 변환
종종 특정 요소만 복사하거나 복사하는 동안 요소를 변환해야 할 수 있습니다. 이러한 기술을 살펴보겠습니다.
tuple_selective_copying.py라는 새 파일을 다음 내용으로 만듭니다.
original_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print("Original tuple:", original_tuple)
## Copying a slice (subset) of the tuple
partial_copy = original_tuple[2:7] ## Elements from index 2 to 6
print("\nPartial copy (indexes 2-6):", partial_copy)
## Copying with step
step_copy = original_tuple[::2] ## Every second element
print("Copy with step of 2:", step_copy)
## Copying in reverse order
reverse_copy = original_tuple[::-1]
print("Reversed copy:", reverse_copy)
## Transforming elements while copying using a generator expression
doubled_copy = tuple(x * 2 for x in original_tuple)
print("\nCopy with doubled values:", doubled_copy)
## Copying only even numbers
even_copy = tuple(x for x in original_tuple if x % 2 == 0)
print("Copy with only even numbers:", even_copy)
## Creating a new tuple by combining parts of the original tuple
first_part = original_tuple[:3]
last_part = original_tuple[-3:]
combined_copy = first_part + last_part
print("\nCombined copy (first 3 + last 3):", combined_copy)
- 파일을 저장하고 실행합니다.
python3 /home/labex/project/tuple_selective_copying.py
예상 출력:
Original tuple: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Partial copy (indexes 2-6): (3, 4, 5, 6, 7)
Copy with step of 2: (1, 3, 5, 7, 9)
Reversed copy: (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
Copy with doubled values: (2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
Copy with only even numbers: (2, 4, 6, 8, 10)
Combined copy (first 3 + last 3): (1, 2, 3, 8, 9, 10)
이러한 예제는 기존 튜플에서 새로운 튜플을 만드는 다양한 방법을 보여줍니다. 기억하세요:
- 슬라이싱 (
[start:end], [::step]) 은 요소의 하위 집합으로 새 튜플을 만드는 쉬운 방법입니다.
- 제너레이터 표현식은 요소를 복사하는 동안 변환하는 데 유용합니다.
+ 연산자를 사용한 튜플 연결은 튜플을 결합할 수 있게 해줍니다.
다음 단계에서는 이러한 방법의 성능을 비교하고 더 고급 기술을 살펴보겠습니다.