Hướng dẫn how do you change a tuple element in python? - làm thế nào để bạn thay đổi một phần tử tuple trong python?


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.

Show

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ử »



Cách cắt một danh sách, chuỗi, tuple trong Python

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 loop

Tuy 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 loop

Ngoà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 loop

Vì 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))
# 

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

Nếu bạn muốn thêm các mục mới ở đầu hoặc kết thú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, bạn có thể kết hợp nó 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 như được mô tả ở trên, nhưng nếu bạn muốn chèn một mục mới ở bất kỳ vị trí nào, bạn cần chuyển đổi một bản.

Chuyển đổi

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à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 với
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
8.

  • Chuyển đổi danh sách và tuple cho nhau 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
2

Chèn một mục với

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
9.

  • Thêm một mục vào danh sách trong Python (Phụ lục, mở rộng, chè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 loop
3

Chuyển đổ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
2 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 với
t = (0, 1, 2)

print(t)
# (0, 1, 2)

print(type(t))
# 
2.

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

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

Bạn có thể thay đổi các mục của tuple theo cùng một cách.

Chuyển đổi

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à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à 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.

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

Bạn cũng có thể loại bỏ các mục của tuple theo cùng một cá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
6

Trong ví dụ trên,

t = (0, 1, 2)

print(t)
# (0, 1, 2)

print(type(t))
# 
8 được sử dụng, nhưng bạn cũng có thể sử dụng
t = (0, 1, 2)

print(t)
# (0, 1, 2)

print(type(t))
# 
9 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
8.

  • Xóa một mục khỏi danh sách trong Python (Clear, Pop, Remove, Del)