Bộ dữ liệu không thể thay đổi, có nghĩa là bạn không thể thay đổi, thêm hoặc xóa các mục sau khi bộ tuple được tạo.
Nhưng có một số cách giải quyết.
Thay đổi giá trị tuple
Khi một tuple được tạo, bạn không thể thay đổi giá trị của nó. Bộ dữ liệu không thể thay đổi, hoặc bất biến vì nó cũng được gọi.unchangeable, or immutable as it also is called.
Nhưng có một cách giải quyết. Bạn có thể chuyển đổi tuple thành một danh sách, thay đổi danh sách và chuyển đổi danh sách trở lại thành một bộ.
Thí dụ
Chuyển đổi tuple thành một danh sách để có thể thay đổi nó:
x = ("Apple", "chuối", "Cherry") y = list (x) y [1] = "kiwi" x = tuple (y)
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
Hãy tự mình thử »
Thêm các mục
Vì các bộ dữ liệu là bất biến, chúng không có phương pháp tích hợp In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 7, nhưng có nhiều cách khác để thêm các mặt hàng vào một tuple.
1Convert into a list: Just like the workaround for changing a tuple, you can convert it into a list, add your item(s), and convert it back into a tuple.
Thí dụ
Chuyển đổi tuple thành một danh sách, thêm "màu cam" và chuyển đổi nó trở lại thành một tuple:
Thistuple = ("Apple", "Banana", "Cherry") y = list (Thistuple) Y.Append ("Orange") Thistuple = tuple (y)
y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
Hãy tự mình thử »
Thêm các mụcAdd tuple to a tuple. You are allowed to add tuples to tuples, so if you want to add one item, (or many), create a new tuple with the item(s), and add it to the existing tuple:
Thí dụ
Chuyển đổi tuple thành một danh sách, thêm "màu cam" và chuyển đổi nó trở lại thành một tuple:
Thistuple = ("Apple", "Banana", "Cherry") y = list (Thistuple) Y.Append ("Orange") Thistuple = tuple (y)
y = ("orange",)
thistuple += y
2. Thêm tuple vào một tuple. Bạn được phép thêm bộ dữ liệu vào bộ dữ
Hãy tự mình thử »
Thêm các mục When creating a tuple with only one item, remember to include a comma after the item, otherwise it will not be identified as a tuple.
Vì các bộ dữ liệu là bất biến, chúng không có phương pháp tích hợp In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 7, nhưng có nhiều cách khác để thêm các mặt hàng vào một tuple.
1 You cannot remove items in a tuple.
Thí dụunchangeable, so you cannot remove items from it, but you can use the same workaround as we used for changing and adding tuple items:
Thí dụ
Chuyển đổi tuple thành một danh sách, thêm "màu cam" và chuyển đổi nó trở lại thành một tuple:
Thistuple = ("Apple", "Banana", "Cherry") y = list (Thistuple) Y.Append ("Orange") Thistuple = tuple (y)
y = list(thistuple)
y.remove("apple")
thistuple = tuple(y)
Hãy tự mình thử »
Thêm các mục
Thí dụ
Chuyển đổi tuple thành một danh sách, thêm "màu cam" và chuyển đổi nó trở lại thành một tuple:
Thistuple = ("Apple", "Banana", "Cherry") y = list (Thistuple) Y.Append ("Orange") Thistuple = tuple (y)
del thistuple
print(thistuple) #this will raise an error because the tuple no longer exists
Hãy tự mình thử »
Chà, như Trufa đã hiển thị, về cơ bản có hai cách thay thế phần tử của một Tuple ở một chỉ mục nhất định. Hoặc chuyển đổi bộ tuple thành một danh sách, thay thế phần tử và chuyển đổi trở lại hoặc xây dựng một tuple mới bằng cách kết hợp.
In [1]: def replace_at_index1(tup, ix, val): ...: lst = list(tup) ...: lst[ix] = val ...: return tuple(lst) ...: In [2]: def replace_at_index2(tup, ix, val): ...: return tup[:ix] + (val,) + tup[ix+1:] ...:Vì vậy, phương pháp nào tốt hơn, đó là, nhanh hơn?
Nó chỉ ra rằng đối với các bộ dữ liệu ngắn (trên Python 3.3), sự kết hợp thực sự nhanh hơn!
In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loopTuy nhiên, nếu chúng ta nhìn vào các bộ dữ liệu dài hơn, việc chuyển đổi danh sách là cách để đi:
In [6]: k = tuple(range(1000)) In [7]: %timeit replace_at_index1(k, 500, 99) 100000 loops, best of 3: 9.08 µs per loop In [8]: %timeit replace_at_index2(k, 500, 99) 100000 loops, best of 3: 10.1 µs per loopĐối với các bộ dữ liệu rất dài, chuyển đổi danh sách là tốt hơn đáng kể!
In [9]: m = tuple(range(1000000)) In [10]: %timeit replace_at_index1(m, 500000, 99) 10 loops, best of 3: 26.6 ms per loop In [11]: %timeit replace_at_index2(m, 500000, 99) 10 loops, best of 3: 35.9 ms per loopNgoài ra, hiệu suất của phương pháp nối phụ thuộc vào chỉ số mà chúng tôi thay thế phần tử. Đối với phương thức danh sách, chỉ mục là không liên quan.
In [12]: %timeit replace_at_index1(m, 900000, 99) 10 loops, best of 3: 26.6 ms per loop In [13]: %timeit replace_at_index2(m, 900000, 99) 10 loops, best of 3: 49.2 ms per loopVì vậy: nếu bộ của bạn ngắn, lát và nối. Nếu nó dài, hãy chuyển đổi danh sách!
Trong Python, vì In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9 là bất biến, bạn không thể cập nhật nó, tức là, bạn không thể thêm, thay đổi hoặc loại bỏ các mục (phần tử) trong In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9.
In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9 đại diện cho dữ liệu mà bạn không cần cập nhật, vì vậy bạn nên sử dụng In [6]: k = tuple(range(1000)) In [7]: %timeit replace_at_index1(k, 500, 99) 100000 loops, best of 3: 9.08 µs per loop In [8]: %timeit replace_at_index2(k, 500, 99) 100000 loops, best of 3: 10.1 µs per loop 2 thay vì In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9 nếu bạn cần cập nhật nó. Tuy nhiên, nếu bạn thực sự cần cập nhật In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9, bạn có thể chuyển đổi nó thành In [6]: k = tuple(range(1000)) In [7]: %timeit replace_at_index1(k, 500, 99) 100000 loops, best of 3: 9.08 µs per loop In [8]: %timeit replace_at_index2(k, 500, 99) 100000 loops, best of 3: 10.1 µs per loop 2, cập nhật nó và sau đó biến nó trở lại thành In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9.
Bài viết này mô tả các nội dung sau đây.
- In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9 là bất biến
- Nối một mục vào In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
- Thêm/chèn các mục vào In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
- Thay đổi mục trong In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
- Xóa các mục trong In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
Lưu ý rằng, mặc dù các từ như "thêm", "thay đổi" và "loại bỏ" được sử dụng để thuận tiện, trong thực tế, một đối tượng mới được tạo và đối tượng gốc không được cập nhật.
In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9 là bất biến
Nối một mục vào In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
t = (0, 1, 2) print(t) # (0, 1, 2) print(type(t)) # <class 'tuple'>
Thêm/chèn các mục vào In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
- Thay đổi mục trong In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
print(t[0]) # 0 print(t[:2]) # (0, 1)
Xóa các mục trong In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
# t[0] = 100 # TypeError: 'tuple' object does not support item assignment
Lưu ý rằng, mặc dù các từ như "thêm", "thay đổi" và "loại bỏ" được sử dụng để thuận tiện, trong thực tế, một đối tượng mới được tạo và đối tượng gốc không được cập nhật.
# t.append(100) # AttributeError: 'tuple' object has no attribute 'append'
Nối một mục vào In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
Thêm/chèn các mục vào In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
t_add = t + (3, 4, 5) print(t_add) # (0, 1, 2, 3, 4, 5) print(t) # (0, 1, 2)
Thay đổi mục trong In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 0
Xóa các mục trong In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 1
Lưu ý rằng, mặc dù các từ như "thêm", "thay đổi" và "loại bỏ" được sử dụng để thuận tiện, trong thực tế, một đối tượng mới được tạo và đối tượng gốc không được cập nhật.
- Sử dụng bộ tuple sau làm ví dụ.
Thêm/chèn các mục vào In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
Thay đổi mục trong In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
Xóa các mục trong In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
- Lưu ý rằng, mặc dù các từ như "thêm", "thay đổi" và "loại bỏ" được sử dụng để thuận tiện, trong thực tế, một đối tượng mới được tạo và đối tượng gốc không được cập nhật.
In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 2
Sử dụng bộ tuple sau làm ví dụ.
- Bạn có thể nhận các phần tử theo chỉ mục In [9]: m = tuple(range(1000000)) In [10]: %timeit replace_at_index1(m, 500000, 99) 10 loops, best of 3: 26.6 ms per loop In [11]: %timeit replace_at_index2(m, 500000, 99) 10 loops, best of 3: 35.9 ms per loop 3 hoặc Slice In [9]: m = tuple(range(1000000)) In [10]: %timeit replace_at_index1(m, 500000, 99) 10 loops, best of 3: 26.6 ms per loop In [11]: %timeit replace_at_index2(m, 500000, 99) 10 loops, best of 3: 35.9 ms per loop 4 như danh sách.
In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 3
Cách cắt một danh sách, chuỗi, tuple trong Python
In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 4
Vì In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9 là bất biến, bạn không thể gán một giá trị mới cho một phần tử.
Các phương thức phá hủy (= các phương thức cập nhật đối tượng gốc), chẳng hạn như In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 7 trong In [6]: k = tuple(range(1000)) In [7]: %timeit replace_at_index1(k, 500, 99) 100000 loops, best of 3: 9.08 µs per loop In [8]: %timeit replace_at_index2(k, 500, 99) 100000 loops, best of 3: 10.1 µs per loop 2 không được xác định trong In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9.
In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9 là bất biến, nhưng bạn có thể kết hợp nhiều bộ dữ liệu với toán tử In [12]: %timeit replace_at_index1(m, 900000, 99) 10 loops, best of 3: 26.6 ms per loop In [13]: %timeit replace_at_index2(m, 900000, 99) 10 loops, best of 3: 49.2 ms per loop 1. Tại thời điểm này, đối tượng ban đầu vẫn không thay đổi và một đối tượng mới được tạo ra.
In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 5
Xóa các mục trong In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 9
Lưu ý rằng, mặc dù các từ như "thêm", "thay đổi" và "loại bỏ" được sử dụng để thuận tiện, trong thực tế, một đối tượng mới được tạo và đối tượng gốc không được cập nhật.
In [3]: d = tuple(range(10)) In [4]: %timeit replace_at_index1(d, 5, 99) 1000000 loops, best of 3: 872 ns per loop In [5]: %timeit replace_at_index2(d, 5, 99) 1000000 loops, best of 3: 642 ns per loop 6
Sử dụng bộ tuple sau làm ví dụ.
- Bạn có thể nhận các phần tử theo chỉ mục In [9]: m = tuple(range(1000000)) In [10]: %timeit replace_at_index1(m, 500000, 99) 10 loops, best of 3: 26.6 ms per loop In [11]: %timeit replace_at_index2(m, 500000, 99) 10 loops, best of 3: 35.9 ms per loop 3 hoặc Slice In [9]: m = tuple(range(1000000)) In [10]: %timeit replace_at_index1(m, 500000, 99) 10 loops, best of 3: 26.6 ms per loop In [11]: %timeit replace_at_index2(m, 500000, 99) 10 loops, best of 3: 35.9 ms per loop 4 như danh sách.