Phương thức phân chia chuỗi python trong Python chia một chuỗi thành một danh sách các chuỗi sau khi phá chuỗi đã cho bằng cách phân cách được chỉ định. in Python split a string into a list of strings after breaking the given string by the specified separator.
DEVERATOR: Đây là một dấu phân cách. Chuỗi chia tách tại phân tách được chỉ định này. Nếu không được cung cấp thì bất kỳ không gian trắng là một dấu tách. This is a delimiter. The string splits at this specified separator. If is not provided then any white space is a separator.
MAXSplit: Đó là một số, cho chúng ta biết chia chuỗi thành tối đa số lần được cung cấp. Nếu nó không được cung cấp thì mặc định là -1 có nghĩa là không có giới hạn. It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then the default is -1 that means there is no limit.
Trả về: Trả về một danh sách các chuỗi sau khi phá chuỗi đã cho bởi bộ phân cách được chỉ định. Returns a list of strings after breaking the given string by the specified separator.
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
0
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
1
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
2
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
3
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
4
Output:
['one', 'two', 'three']
Ví dụ 1: Ví dụ để chứng minh chức năng Split () hoạt động như thế nào Example to demonstrate how split() function works
Ở đây chúng tôi đang sử dụng chức năng phân chia chuỗi python () để chia các chuỗi khác nhau thành một danh sách, được phân tách bởi các ký tự khác nhau trong mỗi trường hợp.
Python3
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
7
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
3
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
Ví dụ 2: Ví dụ để chứng minh chức năng phân tách () hoạt động như thế nào khi MaxSplit được chỉ định Example to demonstrate how split() function works when maxsplit is specified
Tham số MaxSplit được sử dụng để kiểm soát số lượng phân tách để trả về sau khi chuỗi được phân tích cú pháp. Ngay cả khi có nhiều lần phân tách có thể, nó sẽ chỉ thực hiện tối đa số lượng phân tách theo định nghĩa của tham số MaxSplit.
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
Chia một chuỗi thành một danh sách trong đó mỗi từ là một mục danh sách:
TXT = "Chào mừng đến với rừng rậm"
x = txt.split ()
in (x)
Hãy tự mình thử »
Định nghĩa và cách sử dụng
Phương thức
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
2 chia một chuỗi vào một danh sách.
Bạn có thể chỉ định phân tách, dấu phân cách mặc định là bất kỳ khoảng trắng nào.
Lưu ý: Khi MaxSplit được chỉ định, danh sách sẽ chứa số lượng phần tử được chỉ định cộng với một. When maxsplit is specified, the list will contain the specified number of elements plus one.
Cú pháp
String.Split (phân tách, MaxSplit)
Giá trị tham số
Tham số
Sự mô tả
máy tách biệt
Không bắt buộc. Chỉ định phân tách để sử dụng khi chia chuỗi. Theo mặc định, bất kỳ khoảng trắng nào cũng là một dấu tách
MaxSplit
Không bắt buộc. Chỉ định số lượng để làm. Giá trị mặc định là -1, là "tất cả các lần xuất hiện"
Nhiều ví dụ hơn
Thí dụ
Chia một chuỗi thành một danh sách trong đó mỗi từ là một mục danh sách:
TXT = "Chào mừng đến với rừng rậm"
x = txt.split ()
in (x)
Hãy tự mình thử »
Thí dụ
Chia một chuỗi thành một danh sách trong đó mỗi từ là một mục danh sách:
TXT = "Chào mừng đến với rừng rậm"
x = txt.split ()
in (x)
Hãy tự mình thử »
Thí dụ
Chia một chuỗi thành một danh sách trong đó mỗi từ là một mục danh sách:
TXT = "Chào mừng đến với rừng rậm"
x = txt.split () x = txt.split("#", 1)
in (x)
Hãy tự mình thử »
Phương thức chuỗi
Bài viết này mô tả cách phân chia các chuỗi theo phân định, ngắt dòng, biểu thức thông thường và số lượng ký tự trong Python.
Chia theo dấu phân cách:
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
2
Chỉ định dấu phân cách:
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
4
Chỉ định số lượng phân tách tối đa:
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
5
Chia từ bên phải bởi DELIMITER:
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
6
Chia theo dòng phá vỡ:
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
7
Tách bằng regex:
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
8
Chia tách bởi nhiều trình phân cách khác nhau
Concatenate một danh sách các chuỗi
Chia dựa trên số lượng ký tự: Slice
Sử dụng lát cắt để phân chia chuỗi dựa trên số lượng ký tự.
Chuỗi Concatenate trong Python (+ toán tử, tham gia, v.v.)
Chia dựa trên số lượng ký tự: Slice
Chia theo dấu phân cách: print(s_lines.rsplit('\n', 1))
# ['one\ntwo\nthree', 'four']
print(s_lines.rsplit('\n', 1)[0])
# one
# two
# three
print(s_lines.rsplit('\n', 1)[1])
# four
2
Chỉ định dấu phân cách:
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
4
Chỉ định số lượng phân tách tối đa:
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
5
Chia từ bên phải bởi DELIMITER:
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
6
Một danh sách các từ được trả về.
s_blank='one two three\nfour\tfive'print(s_blank)# one two three# four fiveprint(s_blank.split())# ['one', 'two', 'three', 'four', 'five']print(type(s_blank.split()))# <class 'list'>
Sử dụng
print(s_lines.rsplit('\n',2)[0])# one# two
3, được mô tả bên dưới, để kết hợp danh sách thành một chuỗi.
Chỉ định dấu phân cách: print(s_lines.rsplit('\n', 1))
# ['one\ntwo\nthree', 'four']
print(s_lines.rsplit('\n', 1)[0])
# one
# two
# three
print(s_lines.rsplit('\n', 1)[1])
# four
4
Chỉ định một dấu phân cách cho tham số đầu tiên
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
Nếu bạn muốn chỉ định nhiều dấu phân cách, hãy sử dụng các biểu thức chính quy như mô tả sau.
Chỉ định số lượng phân tách tối đa: print(s_lines.rsplit('\n', 1))
# ['one\ntwo\nthree', 'four']
print(s_lines.rsplit('\n', 1)[0])
# one
# two
# three
print(s_lines.rsplit('\n', 1)[1])
# four
5
Chỉ định số lượng phân tách tối đa cho tham số thứ hai
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
5.
Nếu
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
5 được đưa ra, nhiều nhất, việc chia tách
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
Chia từ bên phải bởi DELIMITER: print(s_lines.rsplit('\n', 1))
# ['one\ntwo\nthree', 'four']
print(s_lines.rsplit('\n', 1)[0])
# one
# two
# three
print(s_lines.rsplit('\n', 1)[1])
# four
6
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
6 phân tách từ bên phải của chuỗi.
str.rsplit () - Python 3.7.3 Tài liệu
Kết quả khác với
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
2 chỉ khi tham số thứ hai
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
5 được đưa ra.
Theo cách tương tự như
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
2, nếu bạn muốn xóa dòng cuối cùng, hãy sử dụng
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
6.
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
Để xóa hai dòng cuối cùng:
print(s_lines.rsplit('\n',2)[0])# one# two
Chia theo dòng phá vỡ: print(s_lines.rsplit('\n', 1))
# ['one\ntwo\nthree', 'four']
print(s_lines.rsplit('\n', 1)[0])
# one
# two
# three
print(s_lines.rsplit('\n', 1)[1])
# four
7
Ngoài ra còn có một
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
7 để phân tách theo ranh giới dòng.
Str.SplitLines () - Python 3.7.3 Tài liệu
Như trong các ví dụ trước,
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
2 và
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
6 được chia theo mặc định với khoảng trắng bao gồm ngắt dòng và bạn cũng có thể chỉ định ngắt dòng với tham số
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
4.
Tuy nhiên, thường tốt hơn là sử dụng
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
7.
Ví dụ: Chuỗi phân chia chứa
print(s_lines.rsplit('\n',2)[0])# one# two
1 (LF, được sử dụng trong HĐH UNIX bao gồm Mac) và
Xem bài viết sau đây cho các hoạt động khác với các lần phá vỡ dòng.
Xử lý các lần phá vỡ dòng (Newlines) trong Python
Tách bằng regex: print(s_lines.rsplit('\n', 1))
# ['one\ntwo\nthree', 'four']
print(s_lines.rsplit('\n', 1)[0])
# one
# two
# three
print(s_lines.rsplit('\n', 1)[1])
# four
8
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
2 và
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
6 chỉ phân chia khi
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
4 hoàn toàn phù hợp.
Nếu bạn muốn chia một chuỗi phù hợp với biểu thức thông thường (regex) thay vì kết hợp hoàn hảo, hãy sử dụng
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
2 của mô -đun RE.
Re.Split () - Hoạt động biểu thức thường xuyên - Python 3.7.3 Tài liệu
Trong
print(s_lines.rsplit('\n',1))# ['one\ntwo\nthree', 'four']print(s_lines.rsplit('\n',1)[0])# one# two# threeprint(s_lines.rsplit('\n',1)[1])# four
8, chỉ định mẫu regex trong tham số đầu tiên và chuỗi ký tự đích trong tham số thứ hai.
Một ví dụ về sự phân chia theo các số liên tiếp như sau.
32, nó phù hợp với bất kỳ mẫu nào. Tất nhiên, có thể sử dụng các ký tự đặc biệt của regex cho mỗi mẫu, nhưng không sao ngay cả khi chuỗi thông thường được chỉ định như vậy. Bạn có thể chia bằng nhiều chuỗi khác nhau.
Làm thế nào để bạn chia một chuỗi với một dấu phân cách trong Python?
Chia chuỗi trong Python (Delimiter, Line Break, Regex, v.v.)..
Chia theo DELIMITER: Split () Chỉ định DELIMITER: SEP. ....
Chia từ bên phải bằng Delimiter: RSplit ().
Chia theo dòng phá vỡ: Splitlines ().
Tách bằng regex: re.split () ....
Concatenate một danh sách các chuỗi ..
Chia dựa trên số lượng ký tự: Slice ..
Tôi có thể chia một chuỗi bằng hai dấu phân cách python không?
Để phân chia một chuỗi với nhiều trình phân cách trong Python, hãy sử dụng phương thức re.split ().Ở đó.Chức năng chia () chia chuỗi theo từng lần xuất hiện của mẫu.use the re.split() method. The re. split() function splits the string by each occurrence of the pattern.
Làm thế nào để bạn chia một chuỗi thành một danh sách bằng cách sử dụng dấu phân cách dấu phẩy?
Sử dụng str.split () để chuyển đổi chuỗi được phân tách bằng dấu phẩy thành danh sách.Gọi str.Chia (SEP) với "", như SEP để chuyển đổi chuỗi được phân tách bằng dấu phẩy thành một danh sách.split() to convert a comma-separated string to a list. Call str. split(sep) with "," as sep to convert a comma-separated string into a list.
Làm thế nào để bạn chia một chuỗi thành hai chuỗi trong Python?
Sử dụng chức năng split () Hàm này chia chuỗi thành các phần nhỏ hơn.Đây là điều ngược lại với việc hợp nhất nhiều chuỗi thành một.Hàm chia () chứa hai tham số.
This function splits the string into smaller sections. This is the opposite of merging many strings into one. The split () function contains two parameters.