Sử dụng str.split[]
:
>>> "42 0".split[] # or .split[" "]
['42', '0']
Lưu ý rằng str.split[" "]
là giống hệt nhau trong trường hợp này, nhưng sẽ hoạt động khác nhau nếu có nhiều hơn một không gian liên tiếp. Đồng thời, .split[]
chia tách trên tất cả khoảng trắng, không chỉ không gian.
Sử dụng map
thường trông sạch hơn so với việc sử dụng toàn bộ danh sách khi bạn muốn chuyển đổi các mục của Iterables thành các bản tích hợp như
>>> map[int, "42 0".split[]]
[42, 0]
0, >>> map[int, "42 0".split[]]
[42, 0]
1, >>> map[int, "42 0".split[]]
[42, 0]
2, v.v. trong Python 2:>>> map[int, "42 0".split[]]
[42, 0]
Trong Python 3, map
sẽ trả về một đối tượng lười biếng. Bạn có thể đưa nó vào một danh sách với
>>> map[int, "42 0".split[]]
[42, 0]
4:>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
Sử dụng ký tự băm làm dấu phân cách:
Thí dụ
Chia chuỗi, sử dụng dấu phẩy, theo sau là một không gian, làm dấu phân cách:
txt = "Xin chào, tên tôi là Peter, tôi 26 tuổi"
x = txt.split [","]
in [x]
Hãy tự mình thử »
Sử dụng ký tự băm làm dấu phân cách:
TXT = "Apple#Banana#Cherry#Orange"
x = txt.split ["#"]
Chia chuỗi thành một danh sách với các mục tối đa 2: 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ố
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 chuỗi, sử dụng dấu phẩy, theo sau là một không gian, làm dấu phân cách:
txt = "Xin chào, tên tôi là Peter, tôi 26 tuổi"
x = txt.split [","]
in [x]
Hãy tự mình thử »
Thí dụ
Sử dụng ký tự băm làm dấu phân cách:
TXT = "Apple#Banana#Cherry#Orange"
x = txt.split ["#"]
in [x]
Hãy tự mình thử »
Thí dụ
Sử dụng ký tự băm làm dấu phân cách:
TXT = "Apple#Banana#Cherry#Orange"
x = txt.split ["#"]
x = txt.split["#", 1]
in [x]
Hãy tự mình thử »
Thí dụ
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận The combination of all the above regex functions can be used to perform this particular task. In this we compile a regex and match it to group text and numbers separately into a tuple.
Python3
Đôi khi, chúng ta có một chuỗi, bao gồm văn bản và số [hoặc ngược lại], mà không có bất kỳ sự phân biệt cụ thể nào giữa hai. Có thể có một yêu cầu trong đó chúng tôi yêu cầu tách văn bản khỏi số. Hãy để thảo luận về những cách nhất định trong đó điều này có thể được thực hiện. & NBSP;
Phương thức số 1: Sử dụng re.compile [] + re.match [] + re.groups [] Sự kết hợp của tất cả các hàm Regex trên có thể được sử dụng để thực hiện nhiệm vụ cụ thể này. Trong đó, chúng tôi biên dịch một regex và khớp nó với nhóm và các số riêng biệt thành một tuple. & Nbsp;
>>> map[int, "42 0".split[]]
[42, 0]
6 >>> map[int, "42 0".split[]]
[42, 0]
7>>> map[int, "42 0".split[]]
[42, 0]
8>>> map[int, "42 0".split[]]
[42, 0]
9 >>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
0>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
1>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
2>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
3 >>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
4>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
5 >>> map[int, "42 0".split[]]
[42, 0]
2>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
7>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
8>>> map[int, "42 0".split[]]
[42, 0]
9 The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']0
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']1
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']2
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']4
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']6
>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
5The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']8
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']9
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']1____42
The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
5
9 >>> map[int, "42 0".split[]]
[42, 0]
The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
7
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']
1>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
9The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
0 The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
1The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
2 The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
3
5 The slight modification of regex can provide
the flexibility to reduce the number of regex functions required to perform this particular task. The findall function is alone enough for this task. >>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
Python3
Đôi khi, chúng ta có một chuỗi, bao gồm văn bản và số [hoặc ngược lại], mà không có bất kỳ sự phân biệt cụ thể nào giữa hai. Có thể có một yêu cầu trong đó chúng tôi yêu cầu tách văn bản khỏi số. Hãy để thảo luận về những cách nhất định trong đó điều này có thể được thực hiện. & NBSP;
Phương thức số 1: Sử dụng re.compile [] + re.match [] + re.groups [] Sự kết hợp của tất cả các hàm Regex trên có thể được sử dụng để thực hiện nhiệm vụ cụ thể này. Trong đó, chúng tôi biên dịch một regex và khớp nó với nhóm và các số riêng biệt thành một tuple. & Nbsp;
>>> map[int, "42 0".split[]]
[42, 0]
6 >>> map[int, "42 0".split[]]
[42, 0]
7>>> map[int, "42 0".split[]]
[42, 0]
8>>> map[int, "42 0".split[]]
[42, 0]
9 >>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
0>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
8>>> map[int, "42 0".split[]]
[42, 0]
9 The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']0
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']1
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']2
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']4
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']6
>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
5The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']8
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']9
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']1____42
The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
5
9 >>> map[int, "42 0".split[]]
[42, 0]
The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
7
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']
1>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
9The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
0 The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
1The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
2 The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
3
5>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
Python3
Phương thức số 1: Sử dụng re.compile [] + re.match [] + re.groups [] Sự kết hợp của tất cả các hàm Regex trên có thể được sử dụng để thực hiện nhiệm vụ cụ thể này. Trong đó, chúng tôi biên dịch một regex và khớp nó với nhóm và các số riêng biệt thành một tuple. & Nbsp;
>>> map[int, "42 0".split[]]
[42, 0]
6 >>> map[int, "42 0".split[]]
[42, 0]
7map
4
>>> map[int, "42 0".split[]]
[42, 0]
9map
6map
7
>>> map[int, "42 0".split[]]
[42, 0]
9map
6>>> map[int, "42 0".split[]]
[42, 0]
00>>> map[int, "42 0".split[]]
[42, 0]
9>>> map[int, "42 0".split[]]
[42, 0]
02>>> map[int, "42 0".split[]]
[42, 0]
8>>> map[int, "42 0".split[]]
[42, 0]
9 >>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
0>>> map[int, "42 0".split[]]
[42, 0]
07>>> map[int, "42 0".split[]]
[42, 0]
08>>> map[int, "42 0".split[]]
[42, 0]
09>>> map[int, "42 0".split[]]
[42, 0]
10map
7>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
5>>> map[int, "42 0".split[]]
[42, 0]
9>>> map[int, "42 0".split[]]
[42, 0]
14>>> map[int, "42 0".split[]]
[42, 0]
07>>> map[int, "42 0".split[]]
[42, 0]
16>>> map[int, "42 0".split[]]
[42, 0]
17>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
1>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
2>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
3 >>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
4>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
5 >>> map[int, "42 0".split[]]
[42, 0]
2>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
7>>> map[int, "42 0".split[]]
[42, 0]
23>>> map[int, "42 0".split[]]
[42, 0]
24>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
8>>> map[int, "42 0".split[]]
[42, 0]
9 The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']0
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']1
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']2
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']4
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']6
>>> map[int, "42 0".split[]]
>>> list[map[int, "42 0".split[]]]
[42, 0]
5The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']8
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']9
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']3
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']1____42
The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
5
9 >>> map[int, "42 0".split[]]
[42, 0]
The original string is : Geeks4321
The tuple after the split of string and number : ['Geeks', '4321']
7
The original string is : Geeks4321 The tuple after the split of string and number : ['Geeks', '4321']