Hướng dẫn split a string of numbers python - chia một chuỗi số python

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ố

Tham sốSự mô tả
máy tách biệtKhô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
MaxSplitKhô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

  • Đọc
  • Bàn luận
  • 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]
    
    5
    The 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
    >>> map[int, "42 0".split[]]
    [42, 0]
    
    9
    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']

    >>> map[int, "42 0".split[]]
    
    >>> list[map[int, "42 0".split[]]]
    [42, 0]
    
    1
    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']
    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
    >>> map[int, "42 0".split[]]
    
    >>> list[map[int, "42 0".split[]]]
    [42, 0]
    
    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. 

    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]
    
    5
    The 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
    >>> map[int, "42 0".split[]]
    [42, 0]
    
    9
    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']

    >>> map[int, "42 0".split[]]
    
    >>> list[map[int, "42 0".split[]]]
    [42, 0]
    
    1
    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']
    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
    >>> map[int, "42 0".split[]]
    
    >>> list[map[int, "42 0".split[]]]
    [42, 0]
    
    5

    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]
    
    7

    map4

    >>> map[int, "42 0".split[]]
    [42, 0]
    
    9map6

    map7

    >>> map[int, "42 0".split[]]
    [42, 0]
    
    9map6

    >>> 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]
    
    10map7
    >>> 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]
    
    5
    The 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
    >>> map[int, "42 0".split[]]
    [42, 0]
    
    9
    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']


    Làm thế nào để bạn chia một chuỗi số?

    Chia một chuỗi số ..
    Xóa khoảng trống khỏi một chuỗi đã cho ..
    Di chuyển không gian đến phía trước chuỗi trong một lần chuyển tiếp ..
    Xóa thêm khoảng trống khỏi một chuỗi ..
    Urlify một chuỗi đã cho [thay thế khoảng trắng bằng %20].
    In tất cả các chuỗi có thể được thực hiện bằng cách đặt không gian ..
    Đặt không gian giữa các từ bắt đầu bằng chữ in hoa ..

    Làm thế nào để bạn chia một chuỗi giữa các chữ cái và chữ số trong Python?

    # Phương pháp 1: Re.Split [] Nhập RE.s = '111a222b333c' res = re.chia ['[\ d+]', s] in [res] ....
    # Phương pháp 2: Re.Findall [] Nhập RE.s = '111a222b333c' res = re.findall ['[\ d+| [a-za-z]+]', s] ....
    # Phương thức 3: itertools.groupBy [] từ itertools Nhập nhóm.s = '111a222b333c' res = [''.tham gia [g] cho _, g trong nhóm [s, str ..

    Bài Viết Liên Quan

    Chủ Đề