Sự khác biệt chính giữa các bộ dữ liệu và danh sách là gì?
Sự khác biệt chính giữa các bộ dữ liệu và danh sách là các bộ dữ liệu là các đối tượng và danh sách bất biến là có thể thay đổi. Vì vậy, các bộ dữ liệu không thể được sửa đổi trong khi các danh sách có thể được sửa đổi. Tuples có hiệu quả hơn về bộ nhớ hơn các danh sách và do đó thường nhanh hơn
Làm thế nào để sắp xếp các yếu tố của một tuple?
Tuples Python không có phương pháp sắp xếp. Vì vậy, để sắp xếp các yếu tố của một tuple, hàm tích hợp được sắp xếp có thể được sử dụng bằng cách truyền tuple làm đối số. Hàm trả về một danh sách với các mục của tuple được sắp xếp theo thứ tự tăng dần. Danh sách này có thể được chuyển đổi thành một tuple bằng cách sử dụng Tuple (), thứ tự sắp xếp mặc định là thứ tự tăng dần. Đảo ngược = tham số true sắp xếp các mục theo thứ tự giảm dần.sorted builtin function can be used by passing the tuple as the argument. The function returns a list with the items of tuple sorted in ascending order. This list can be converted into a tuple by using tuple( ) The default sorting order is ascending order. The reverse=True parameter sorts the items in descending order.
Hai phương pháp có sẵn cho một tuple là gì
Tuples Python có hai phương pháp tích hợp:
Làm thế nào để tìm thấy số lần xuất hiện của một phần tử trong một tuple?
my_tuple.count (phần tử) sẽ cung cấp số lần xuất hiện của một phần tử trong my_tuplecount(element) will give the number of occurrence of an element in my_tuple
Làm thế nào để tạo một tuple?
Một bộ gốc trống có thể được tạo bằng cách sử dụng hàm tạo tuple, tuple () hoặc bằng cách sử dụng một cặp dấu ngoặc đơn không có giá trị trong đó. Một tuple chỉ có một yếu tố (singleton tuple) có thể được tạo bằng cách sử dụng dấu phẩy kéo theo đối tượng hoặc bằng cách sử dụng một cặp dấu ngoặc đơn với đối tượng trong chúng. Một tuple có nhiều yếu tố có thể được tạo bởi các phần tử được phân tách bằng dấu phẩy hoặc bằng cách sử dụng một cặp dấu ngoặc đơn với các phần tử được phân tách bằng dấu phẩy trong đó.tuple( ) or by using a pair of parentheses with no values in them.
A tuple with just one element (singleton tuple) can be created by using a
trailing comma after the object or by using a pair of parentheses with the object in them.
A tuple with more than one element can be created by elements separated by commas or by using a pair of parentheses with the elements separated by commas in them.
Làm thế nào để xóa một tuple?
del.my_tuple sẽ xóa my_tuple.my_tuple would delete my_tuple
Làm thế nào để thêm một phần tử vào một tuple?
Vì các bộ dữ liệu là bất biến, các phần tử không thể được thêm/sửa đổi/bị xóa khỏi một tuple.
Làm thế nào để tìm chỉ số của phần tử cụ thể trong một tuple?
my_tuple.index (phần tử, bắt đầu, kết thúc) sẽ trả lại vị trí của lần xuất hiện đầu tiên của phần tử trong tuple và nếu phần tử không được tìm thấy, valueerror sẽ được nâng lên.index(element,start,end) would retun the position of the first occurrence of the element in the tuple and If the element is not found, ValueError is raised.
Làm thế nào để so sánh hai bộ dữ liệu để xem chúng có cùng yếu tố không?
Có thể sử dụng phương pháp CMP của Python Python để so sánh hai bộ dữ liệu để xem chúng có cùng yếu tốcmp method can be used to compare two tuples to see if they have the same elements
Một tuple có thể là một thành viên trong một tuple khác không?
Vâng, bộ dữ liệu có thể được lồng đến bất kỳ độ sâu cần thiết. TUP1 = (1, 2, 3) TUP2 = ('gk', 'nxt') nested_tuple = (TUP1, TUP2)
tup1 = (1, 2, 3)
tup2 = ('gk', 'nxt')
nested_tuple = (tup1, tup2)
Làm thế nào để chuyển đổi một tuple thành một danh sách?
Một tuple có thể được chuyển đổi thành một danh sách bằng cách sử dụng hàm tạo danh sách với bộ tuple làm danh sách lập luận (my_tuple)
list(my_tuple)
Làm thế nào để chuyển đổi một danh sách thành một tuple?
Một danh sách có thể được chuyển đổi thành một bộ xử lý bằng cách sử dụng hàm tạo tuple với danh sách làm đối số (my_list)
tuple(my_list)
Một tuple là gì?
Một tuple là một bộ sưu tập các đối tượng bất biến, có thứ tự. Người tạo Python sườn dự định các bộ dữ liệu không đồng nhất. Tuples là cách đóng gói các mẩu thông tin không đồng nhất của Python trong một đối tượng tổng hợp. Ví dụ: socket = ('www.python.org', 80) tập hợp một chuỗi và một số để cặp máy chủ/cổng có thể được truyền xung quanh dưới dạng ổ cắm, một đối tượng tổng hợpsocket = ('www.python.org', 80) brings together a string and a number so that the host/port pair can be passed around as a socket, a composite object
Một tuple có thể là một thành viên trong chính nó?
Có, nhưng nó sẽ tạo ra một tham chiếu tròn
Python Tuple là một tập hợp các đối tượng được phân tách bằng dấu phẩy. Theo một số cách, một tuple tương tự như một danh sách về việc lập chỉ mục, các đối tượng lồng nhau và sự lặp lại nhưng một tuple là bất biến, không giống như các danh sách có thể thay đổi. is a collection of objects separated by commas. In some ways, a tuple is similar to a list in terms of indexing, nested objects, and repetition but a tuple is immutable, unlike lists which are mutable.
Tạo các bộ dữ liệu python
Để tạo một tuple, chúng tôi sẽ sử dụng các toán tử ().
Python
Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks3Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks4 Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks8Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks1
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks3
Output:
('Geeks', 'for', 'Geeks')Lưu ý: Trong trường hợp bạn tạo một tuple với một phần tử duy nhất, hãy đảm bảo thêm dấu phẩy sau phần tử. & NBSP;
Truy cập các giá trị trong các bộ dữ liệu Python
Phương pháp 1: Sử dụng chỉ số tích cực
Sử dụng dấu ngoặc vuông, chúng ta có thể nhận được các giá trị từ các bộ dữ liệu trong Python.
Python3
Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks3Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks4 Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks8Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks1
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5(0, 1, 2, 3, 'python', 'geek')5(0, 1, 2, 3, 'python', 'geek')6(0, 1, 2, 3, 'python', 'geek')7(0, 1, 2, 3, 'python', 'geek')8
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5((0, 1, 2, 3), ('python', 'geek'))1(0, 1, 2, 3, 'python', 'geek')6((0, 1, 2, 3), ('python', 'geek'))3(0, 1, 2, 3, 'python', 'geek')8
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5((0, 1, 2, 3), ('python', 'geek'))7(0, 1, 2, 3, 'python', 'geek')6((0, 1, 2, 3), ('python', 'geek'))9(0, 1, 2, 3, 'python', 'geek')8
Output:
Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = GeeksLưu ý: Trong trường hợp bạn tạo một tuple với một phần tử duy nhất, hãy đảm bảo thêm dấu phẩy sau phần tử. & NBSP;
Truy cập các giá trị trong các bộ dữ liệu Python
Python3
Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks3Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks4 Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks8Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks1
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5Traceback (most recent call last): File "e0eaddff843a8695575daec34506f126.py", line 3, in tuple1[0]=4 TypeError: 'tuple' object does not support item assignment2(0, 1, 2, 3, 'python', 'geek')6Traceback (most recent call last): File "e0eaddff843a8695575daec34506f126.py", line 3, in tuple1[0]=4 TypeError: 'tuple' object does not support item assignment4Traceback (most recent call last): File "e0eaddff843a8695575daec34506f126.py", line 3, in tuple1[0]=4 TypeError: 'tuple' object does not support item assignment5(0, 1, 2, 3, 'python', 'geek')8
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5Traceback (most recent call last): File "e0eaddff843a8695575daec34506f126.py", line 3, in tuple1[0]=4 TypeError: 'tuple' object does not support item assignment9(0, 1, 2, 3, 'python', 'geek')6Traceback (most recent call last): File "e0eaddff843a8695575daec34506f126.py", line 3, in tuple1[0]=4 TypeError: 'tuple' object does not support item assignment4((0, 1, 2, 3), ('python', 'geek'))9(0, 1, 2, 3, 'python', 'geek')8
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5(1, 2, 3) (3, 2, 1, 0) (2, 3)6(0, 1, 2, 3, 'python', 'geek')6Traceback (most recent call last): File "e0eaddff843a8695575daec34506f126.py", line 3, in tuple1[0]=4 TypeError: 'tuple' object does not support item assignment4((0, 1, 2, 3), ('python', 'geek'))3(0, 1, 2, 3, 'python', 'geek')8
Output:
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = GeeksLưu ý: Trong trường hợp bạn tạo một tuple với một phần tử duy nhất, hãy đảm bảo thêm dấu phẩy sau phần tử. & NBSP;
Truy cập các giá trị trong các bộ dữ liệu Python
Python
Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks3Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks4 Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks8Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks1
Lưu ý: Trong trường hợp bạn tạo một tuple với một phần tử duy nhất, hãy đảm bảo thêm dấu phẩy sau phần tử. & NBSP;
Truy cập các giá trị trong các bộ dữ liệu Python
Output:
(0, 1, 2, 3, 'python', 'geek')Phương pháp 1: Sử dụng chỉ số tích cựcNesting of Python Tuples
Python
Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks3Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks4 Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks8Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks1
Lưu ý: Trong trường hợp bạn tạo một tuple với một phần tử duy nhất, hãy đảm bảo thêm dấu phẩy sau phần tử. & NBSP;
Truy cập các giá trị trong các bộ dữ liệu Python
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks25
Phương pháp 1: Sử dụng chỉ số tích cực
((0, 1, 2, 3), ('python', 'geek'))Sử dụng dấu ngoặc vuông, chúng ta có thể nhận được các giá trị từ các bộ dữ liệu trong Python.Repetition Python Tuples
Python
Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks3Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks4 Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks8Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks1
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks25
Output:
('python', 'python', 'python')Lưu ý: Trong trường hợp bạn tạo một tuple với một phần tử duy nhất, hãy đảm bảo thêm dấu phẩy sau phần tử. & NBSP;
Truy cập các giá trị trong các bộ dữ liệu Python
Python
Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks3Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks4 Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks8Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks6Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks1
Lưu ý: Trong trường hợp bạn tạo một tuple với một phần tử duy nhất, hãy đảm bảo thêm dấu phẩy sau phần tử. & NBSP;
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks52
Output:
Traceback (most recent call last): File "e0eaddff843a8695575daec34506f126.py", line 3, in tuple1[0]=4 TypeError: 'tuple' object does not support item assignmentTruy cập các giá trị trong các bộ dữ liệu PythonSlicing Python Tuples
Python
Phương pháp 1: Sử dụng chỉ số tích cực
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks65((0, 1, 2, 3), ('python', 'geek'))3Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks67
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks69Traceback (most recent call last): File "e0eaddff843a8695575daec34506f126.py", line 3, in tuple1[0]=4 TypeError: 'tuple' object does not support item assignment4((0, 1, 2, 3), ('python', 'geek'))3(0, 1, 2, 3, 'python', 'geek')8
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks65((0, 1, 2, 3), ('python', 'geek'))9Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks76Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks50(0, 1, 2, 3, 'python', 'geek')8
Output:
(1, 2, 3) (3, 2, 1, 0) (2, 3)Sử dụng dấu ngoặc vuông, chúng ta có thể nhận được các giá trị từ các bộ dữ liệu trong Python.Deleting a Tuple
Python
Phương pháp 2: Sử dụng chỉ số âm.
Trong các phương thức trên, chúng tôi sử dụng chỉ số dương để truy cập giá trị trong Python và ở đây chúng tôi sẽ sử dụng chỉ số -ve trong [].
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks25
Error:
Traceback (most recent call last): File "d92694727db1dc9118a5250bf04dafbd.py", line 6, in <module> print(tuple3) NameError: name 'tuple3' is not definedOutput:
(0, 1)Kết nối các bộ dữ liệu pythonFinding Length of a Tuple
Python
Lưu ý: Trong trường hợp bạn tạo một tuple với một phần tử duy nhất, hãy đảm bảo thêm dấu phẩy sau phần tử. & NBSP;
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks99Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks00
Output:
Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks0Truy cập các giá trị trong các bộ dữ liệu PythonConverting list to a Tuple
Python
Phương pháp 1: Sử dụng chỉ số tích cực
Sử dụng dấu ngoặc vuông, chúng ta có thể nhận được các giá trị từ các bộ dữ liệu trong Python.
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks12Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5(0, 1)5Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks19
Output:
Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks1Phương pháp 2: Sử dụng chỉ số âm.
Trong các phương thức trên, chúng tôi sử dụng chỉ số dương để truy cập giá trị trong Python và ở đây chúng tôi sẽ sử dụng chỉ số -ve trong [].Tuples in a loop
Python
Kết nối các bộ dữ liệu python
Để kết hợp các python tuple, chúng tôi sẽ sử dụng các toán tử cộng (+).
Traceback (most recent call last): File "d92694727db1dc9118a5250bf04dafbd.py", line 6, in <module> print(tuple3) NameError: name 'tuple3' is not defined1Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks4 Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5(0, 1, 2, 3, 'python', 'geek')7Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7((0, 1, 2, 3), ('python', 'geek'))3Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7((0, 1, 2, 3), ('python', 'geek'))9Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7Traceback (most recent call last): File "e0eaddff843a8695575daec34506f126.py", line 3, in tuple1[0]=4 TypeError: 'tuple' object does not support item assignment5Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks1
(0, 1)2Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks4 Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks5(0, 1)5Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks7(0, 1)7Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks1
Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks35Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks2Value in Var[-3] = Geeks Value in Var[-2] = for Value in Var[-1] = Geeks41
Output:
Value in Var[0] = Geeks Value in Var[1] = for Value in Var[2] = Geeks2