Hàm phân vùng chia toàn bộ chuỗi thành 3 phần và trả về dưới dạng một bộ gồm 3 phần tử
- Chuỗi trước dấu phân cách
- dải phân cách
- Chuỗi sau dấu phân cách
Hàm này chỉ tách một lần và nó sẽ không tách nhiều lần. Vì vậy, nó tách ra ở lần xuất hiện đầu tiên của dấu tách
cú phápsợi dây. phân vùng [dấu phân cách]
fullstring = "partition function is a type of split function in python."
result = fullstring.partition["function"]
print[result]
print[type[result]]
đầu ra
['partition ', 'function', ' is a type of split function in python.']
2. phân vùng []Hàm này hoạt động tương tự như hàm partition[] ngoại trừ việc nó xem xét lần xuất hiện cuối cùng của dấu tách
fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
đầu ra
['partition function is a type of split ', 'function', ' in python.']
3. đường phân chia []Chức năng này chỉ phân tách bằng cách ngắt dòng
Điều này sẽ không chấp nhận bất kỳ dấu tách nào, thay vào đó, nó sẽ chấp nhận một đối số boolean tùy chọn khác “keepends”
Nếu keepends là True thì nó sẽ trả về ký tự ngắt dòng cùng với phần bên trái của ngắt dòng
Chuyển đổi giữa các kiểu dữ liệu là một vấn đề rất phổ biến mà người ta có thể gặp phải khi lập trình. Có thể xảy ra sự cố khi chúng ta cần chuyển đổi một chuỗi số nguyên thành một bộ. Hãy thảo luận về những cách nhất định để thực hiện điều này.
Phương pháp số 1. Sử dụng map[] + int + split[] + tuple[]
Phương pháp này có thể được sử dụng để giải quyết nhiệm vụ cụ thể này. Trong phần này, chúng tôi chỉ tách từng phần tử của chuỗi và chuyển đổi thành danh sách và sau đó chúng tôi chuyển đổi danh sách thành bộ dữ liệu kết quả.
Python3
['partition ', 'function', ' is a type of split function in python.']
8['partition ', 'function', ' is a type of split function in python.']
9fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
0
fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
1fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
2_______23 fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
4____40The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]1
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]2
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]3
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]1
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]5
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]1
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]7
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]1
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]9
fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
4 Cho trước một bộ, nhiệm vụ là chia nó thành các nhóm n. Hãy thảo luận về một vài phương pháp để hoàn thành một nhiệm vụ nhất định
Phương pháp số 1. Sử dụng hàm liệt kê và phạm vi
fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
6fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
7fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
8fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
9['partition function is a type of split ', 'function', ' in python.']
0['partition function is a type of split ', 'function', ' in python.']
1fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
3 ['partition function is a type of split ', 'function', ' in python.']
3_______34['partition function is a type of split ', 'function', ' in python.']
5fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
70['partition function is a type of split ', 'function', ' in python.']
5fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
72['partition function is a type of split ', 'function', ' in python.']
5fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
74['partition function is a type of split ', 'function', ' in python.']
5fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
76['partition function is a type of split ', 'function', ' in python.']
5fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
78['partition function is a type of split ', 'function', ' in python.']
5fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
72['partition function is a type of split ', 'function', ' in python.']
5fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
82['partition function is a type of split ', 'function', ' in python.']
5fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
84fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
85['partition function is a type of split ', 'function', ' in python.']
5fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
87['partition function is a type of split ', 'function', ' in python.']
5['partition function is a type of split ', 'function', ' in python.']
4['partition function is a type of split ', 'function', ' in python.']
5['partition function is a type of split ', 'function', ' in python.']
4['partition function is a type of split ', 'function', ' in python.']
5fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
93['partition function is a type of split ', 'function', ' in python.']
5fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
95['partition function is a type of split ', 'function', ' in python.']
5fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
97['partition function is a type of split ', 'function', ' in python.']
5fullstring = "partition function is a type of split function in python."
result = fullstring.rpartition["function"]
print[result]
99['partition function is a type of split ', 'function', ' in python.']
00 Trong Python, bạn có thể chia các bộ dữ liệu thành nhiều biến bằng cách giải nén bộ dữ liệu. Bạn có thể giải nén các bộ có tên biến được phân tách bằng dấu phẩy
['partition ', 'function', ' is a type of split function in python.']
4Trong Python, bộ dữ liệu là tập hợp các đối tượng được sắp xếp theo thứ tự và có thể thay đổi. Khi làm việc với các bộ dữ liệu, sẽ rất hữu ích nếu có thể giải nén các giá trị của các bộ dữ liệu và tạo các biến mới
Bạn có thể giải nén các bộ có tên biến được phân tách bằng dấu phẩy
Để tách một bộ, chỉ cần liệt kê các tên biến được phân tách bằng dấu phẩy ở phía bên trái của dấu bằng và sau đó là một bộ ở phía bên tay phải
Dưới đây là một ví dụ về việc tách một bộ dữ liệu thành ba biến trong Python
['partition ', 'function', ' is a type of split function in python.']
4Sử dụng Tuple Unpacking để hoán đổi giá trị trong Python
Một ứng dụng của việc tách một bộ là khi bạn muốn hoán đổi hiệu quả giá trị của hai biến trong Python. Chúng ta có thể hoán đổi các giá trị với việc giải nén tuple theo cách sau
['partition ', 'function', ' is a type of split function in python.']
6Tách danh sách các bộ dữ liệu thành danh sách trong Python
Khi làm việc với danh sách các bộ dữ liệu, đôi khi có thể hữu ích khi có thể chia danh sách các bộ dữ liệu thành các danh sách chứa các giá trị của từng phần tử bộ dữ liệu
Chúng ta có thể sử dụng giải nén bộ dữ liệu để tạo danh sách mới từ danh sách bộ dữ liệu theo cách tương tự như chúng ta có thể tạo biến từ bộ dữ liệu
Dưới đây là một ví dụ về cách chia danh sách các bộ dữ liệu thành các danh sách bằng Python
['partition ', 'function', ' is a type of split function in python.']
7Hy vọng bài viết này hữu ích cho bạn trong việc hiểu cách chia một bộ dữ liệu thành nhiều biến bằng Python