Grundlegende Techniken zum Kopieren von Tuple-Elementen
In diesem Schritt werden wir grundlegende Techniken zum Kopieren von Elementen von einem Tupel in ein anderes untersuchen. Da Tupel unveränderlich sind, bedeutet das Kopieren tatsächlich, ein neues Tupel mit denselben oder ausgewählten Elementen zu erstellen.
Erstellen wir eine neue Datei, um mit diesen Techniken zu experimentieren:
- Erstellen Sie eine neue Datei namens
tuple_copying_basics.py im Verzeichnis /home/labex/project
- Fügen Sie den folgenden Code in die Datei ein:
## 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)
- Speichern Sie die Datei und führen Sie sie mit dem folgenden Befehl aus:
python3 /home/labex/project/tuple_copying_basics.py
Sie sollten eine Ausgabe ähnlich dieser sehen:
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
Oft möchten Sie möglicherweise nur bestimmte Elemente kopieren oder Elemente beim Kopieren transformieren. Lassen Sie uns diese Techniken untersuchen:
- Erstellen Sie eine neue Datei namens
tuple_selective_copying.py mit folgendem Inhalt:
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)
- Speichern Sie die Datei und führen Sie sie aus:
python3 /home/labex/project/tuple_selective_copying.py
Erwartete Ausgabe:
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)
Diese Beispiele zeigen verschiedene Möglichkeiten, neue Tupel aus vorhandenen zu erstellen. Denken Sie daran:
- Slicing (
[start:end], [::step]) ist eine einfache Möglichkeit, ein neues Tupel mit einer Teilmenge von Elementen zu erstellen
- Generator Expressions sind nützlich, um Elemente zu transformieren, während Sie sie kopieren
- Tuple-Verkettung mit dem
+-Operator ermöglicht das Kombinieren von Tupeln
Im nächsten Schritt werden wir die Leistung dieser Methoden vergleichen und fortgeschrittenere Techniken untersuchen.