Hướng dẫn how to split a string of numbers in python - cách tách một chuỗi số trong 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]

Phương thức chuỗi


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ử »


Định nghĩa và cách sử dụng

Phương thức

>>> map(int, "42 0".split())
[42, 0]
5 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ệ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ụ

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ụ

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 (",")
x = txt.split("#", 1)

in (x)

Hãy tự mình thử »


Sử dụng ký tự băm làm dấu phân cách:


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]
    
    1.split()9
    >>> map(int, "42 0".split())
    [42, 0]
    
    27
    >>> map(int, "42 0".split())
    
    >>> list(map(int, "42 0".split()))
    [42, 0]
    
    5
    >>> map(int, "42 0".split())
    [42, 0]
    
    2
    The original string is : Geeks4321
    The tuple after the split of string and number : ['Geeks', '4321']
    6

    Đầu ra

    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ố trong Python?

    Đầu vào: test_str = '457336', k = 2 ..
    Đầu ra: [45, 73, 36].
    Giải thích: chia thành 2 số nguyên chữ số ..
    Đầu vào: test_str = '457336', k = 3 ..
    Đầu ra: [457, 336].
    Giải thích: chia thành 3 số nguyên chữ số ..

    Làm cách nào để tách một số khỏi một chuỗi?

    Các bước:..
    Tính độ dài của chuỗi ..
    Quét mọi ký tự (CH) của một chuỗi từng người một.Nếu (CH là một chữ số) thì hãy nối nó trong chuỗi Res1.....
    In tất cả các chuỗi, chúng ta sẽ có một chuỗi chứa một phần số, phần không phải là số khác và phần cuối cùng chứa các ký tự đặc biệt ..

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

    Thư viện tiêu chuẩn Python đi kèm với một hàm để chia các chuỗi: hàm chia ().Hàm này có thể được sử dụng để phân chia chuỗi giữa các ký tự.Hàm chia () có hai tham số.Đầu tiên được gọi là dấu phân cách và nó xác định ký tự nào được sử dụng để phân chia chuỗi.the split() function. This function can be used to split strings between characters. The split() function takes two parameters. The first is called the separator and it determines which character is used to split the string.