Hướng dẫn how do you separate an int from a string in python? - làm thế nào để bạn tách một int khỏi một chuỗi trong python?

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;

    import re

    test_str = "Geeks4321"

    print

    The original string is : Geeks4321
    The tuple after the split of string and number : ('Geeks', '4321')
    0____11
    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')
    5

    The original string is : Geeks4321
    The tuple after the split of string and number : ('Geeks', '4321')
    6=
    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']
    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']
    2221
    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__

    import re
    
    # Format is [(, ), ...]
    ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
           ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
          ('hello X42 I\'m a Y-32.35 string Z30',
           ['42', '-32.35', '30']),
          ('he33llo 42 I\'m a 32 string -30', 
           ['33', '42', '32', '-30']),
          ('h3110 23 cat 444.4 rabbit 11 2 dog', 
           ['3110', '23', '444.4', '11', '2']),
          ('hello 12 hi 89', 
           ['12', '89']),
          ('4', 
           ['4']),
          ('I like 74,600 commas not,500', 
           ['74,600', '500']),
          ('I like bad math 1+2=.001', 
           ['1', '+2', '.001'])]
    
    for s, r in ss:
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
        if rr == r:
            print('GOOD')
        else:
            print('WRONG', rr, 'should be', r)
    
    3=
    import re
    
    # Format is [(, ), ...]
    ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
           ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
          ('hello X42 I\'m a Y-32.35 string Z30',
           ['42', '-32.35', '30']),
          ('he33llo 42 I\'m a 32 string -30', 
           ['33', '42', '32', '-30']),
          ('h3110 23 cat 444.4 rabbit 11 2 dog', 
           ['3110', '23', '444.4', '11', '2']),
          ('hello 12 hi 89', 
           ['12', '89']),
          ('4', 
           ['4']),
          ('I like 74,600 commas not,500', 
           ['74,600', '500']),
          ('I like bad math 1+2=.001', 
           ['1', '+2', '.001'])]
    
    for s, r in ss:
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
        if rr == r:
            print('GOOD')
        else:
            print('WRONG', rr, 'should be', r)
    
    5

    The original string is : Geeks4321
    The tuple after the split of string and number : ('Geeks', '4321')

    print

    import re
    
    # Format is [(, ), ...]
    ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
           ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
          ('hello X42 I\'m a Y-32.35 string Z30',
           ['42', '-32.35', '30']),
          ('he33llo 42 I\'m a 32 string -30', 
           ['33', '42', '32', '-30']),
          ('h3110 23 cat 444.4 rabbit 11 2 dog', 
           ['3110', '23', '444.4', '11', '2']),
          ('hello 12 hi 89', 
           ['12', '89']),
          ('4', 
           ['4']),
          ('I like 74,600 commas not,500', 
           ['74,600', '500']),
          ('I like bad math 1+2=.001', 
           ['1', '+2', '.001'])]
    
    for s, r in ss:
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
        if rr == r:
            print('GOOD')
        else:
            print('WRONG', rr, 'should be', r)
    
    7
    import re
    
    # Format is [(, ), ...]
    ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
           ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
          ('hello X42 I\'m a Y-32.35 string Z30',
           ['42', '-32.35', '30']),
          ('he33llo 42 I\'m a 32 string -30', 
           ['33', '42', '32', '-30']),
          ('h3110 23 cat 444.4 rabbit 11 2 dog', 
           ['3110', '23', '444.4', '11', '2']),
          ('hello 12 hi 89', 
           ['12', '89']),
          ('4', 
           ['4']),
          ('I like 74,600 commas not,500', 
           ['74,600', '500']),
          ('I like bad math 1+2=.001', 
           ['1', '+2', '.001'])]
    
    for s, r in ss:
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
        if rr == r:
            print('GOOD')
        else:
            print('WRONG', rr, 'should be', r)
    
    8
    import re
    
    # Format is [(, ), ...]
    ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
           ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
          ('hello X42 I\'m a Y-32.35 string Z30',
           ['42', '-32.35', '30']),
          ('he33llo 42 I\'m a 32 string -30', 
           ['33', '42', '32', '-30']),
          ('h3110 23 cat 444.4 rabbit 11 2 dog', 
           ['3110', '23', '444.4', '11', '2']),
          ('hello 12 hi 89', 
           ['12', '89']),
          ('4', 
           ['4']),
          ('I like 74,600 commas not,500', 
           ['74,600', '500']),
          ('I like bad math 1+2=.001', 
           ['1', '+2', '.001'])]
    
    for s, r in ss:
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
        if rr == r:
            print('GOOD')
        else:
            print('WRONG', rr, 'should be', r)
    
    9import0 import1
    The original string is : Geeks4321
    The tuple after the split of string and number : ('Geeks', '4321')
    3
    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;

    import re

    test_str = "Geeks4321"

    The original string is : Geeks4321
    The tuple after the split of string and number : ('Geeks', '4321')
    6=
    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']
    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']
    2221
    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__

    import re
    
    # Format is [(, ), ...]
    ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
           ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
          ('hello X42 I\'m a Y-32.35 string Z30',
           ['42', '-32.35', '30']),
          ('he33llo 42 I\'m a 32 string -30', 
           ['33', '42', '32', '-30']),
          ('h3110 23 cat 444.4 rabbit 11 2 dog', 
           ['3110', '23', '444.4', '11', '2']),
          ('hello 12 hi 89', 
           ['12', '89']),
          ('4', 
           ['4']),
          ('I like 74,600 commas not,500', 
           ['74,600', '500']),
          ('I like bad math 1+2=.001', 
           ['1', '+2', '.001'])]
    
    for s, r in ss:
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
        if rr == r:
            print('GOOD')
        else:
            print('WRONG', rr, 'should be', r)
    
    3=
    import re
    
    # Format is [(, ), ...]
    ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
           ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
          ('hello X42 I\'m a Y-32.35 string Z30',
           ['42', '-32.35', '30']),
          ('he33llo 42 I\'m a 32 string -30', 
           ['33', '42', '32', '-30']),
          ('h3110 23 cat 444.4 rabbit 11 2 dog', 
           ['3110', '23', '444.4', '11', '2']),
          ('hello 12 hi 89', 
           ['12', '89']),
          ('4', 
           ['4']),
          ('I like 74,600 commas not,500', 
           ['74,600', '500']),
          ('I like bad math 1+2=.001', 
           ['1', '+2', '.001'])]
    
    for s, r in ss:
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
        if rr == r:
            print('GOOD')
        else:
            print('WRONG', rr, 'should be', r)
    
    5

    The original string is : Geeks4321
    The tuple after the split of string and number : ('Geeks', '4321')

    print

    import re
    
    # Format is [(, ), ...]
    ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
           ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
          ('hello X42 I\'m a Y-32.35 string Z30',
           ['42', '-32.35', '30']),
          ('he33llo 42 I\'m a 32 string -30', 
           ['33', '42', '32', '-30']),
          ('h3110 23 cat 444.4 rabbit 11 2 dog', 
           ['3110', '23', '444.4', '11', '2']),
          ('hello 12 hi 89', 
           ['12', '89']),
          ('4', 
           ['4']),
          ('I like 74,600 commas not,500', 
           ['74,600', '500']),
          ('I like bad math 1+2=.001', 
           ['1', '+2', '.001'])]
    
    for s, r in ss:
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
        if rr == r:
            print('GOOD')
        else:
            print('WRONG', rr, 'should be', r)
    
    7
    import re
    
    # Format is [(, ), ...]
    ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
           ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
          ('hello X42 I\'m a Y-32.35 string Z30',
           ['42', '-32.35', '30']),
          ('he33llo 42 I\'m a 32 string -30', 
           ['33', '42', '32', '-30']),
          ('h3110 23 cat 444.4 rabbit 11 2 dog', 
           ['3110', '23', '444.4', '11', '2']),
          ('hello 12 hi 89', 
           ['12', '89']),
          ('4', 
           ['4']),
          ('I like 74,600 commas not,500', 
           ['74,600', '500']),
          ('I like bad math 1+2=.001', 
           ['1', '+2', '.001'])]
    
    for s, r in ss:
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
        if rr == r:
            print('GOOD')
        else:
            print('WRONG', rr, 'should be', r)
    
    8
    import re
    
    # Format is [(, ), ...]
    ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
           ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
          ('hello X42 I\'m a Y-32.35 string Z30',
           ['42', '-32.35', '30']),
          ('he33llo 42 I\'m a 32 string -30', 
           ['33', '42', '32', '-30']),
          ('h3110 23 cat 444.4 rabbit 11 2 dog', 
           ['3110', '23', '444.4', '11', '2']),
          ('hello 12 hi 89', 
           ['12', '89']),
          ('4', 
           ['4']),
          ('I like 74,600 commas not,500', 
           ['74,600', '500']),
          ('I like bad math 1+2=.001', 
           ['1', '+2', '.001'])]
    
    for s, r in ss:
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
        if rr == r:
            print('GOOD')
        else:
            print('WRONG', rr, 'should be', r)
    
    9import0 import1
    The original string is : Geeks4321
    The tuple after the split of string and number : ('Geeks', '4321')
    3

    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;

    import re

    "Geeks4321"2="Geeks4321"4

    "Geeks4321"5="Geeks4321"4

    "Geeks4321"8=print0

    test_str = "Geeks4321"

    print5print6print7

    print8"Geeks4321"5

    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')
    02

    print5

    The original string is : Geeks4321
    The tuple after the split of string and number : ('Geeks', '4321')
    04
    The original string is : Geeks4321
    The tuple after the split of string and number : ('Geeks', '4321')
    05

    print

    The original string is : Geeks4321
    The tuple after the split of string and number : ('Geeks', '4321')
    0____11
    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')
    5

    The original string is : Geeks4321
    The tuple after the split of string and number : ('Geeks', '4321')
    11

    The original string is : Geeks4321
    The tuple after the split of string and number : ('Geeks', '4321')
    12

    The original string is : Geeks4321
    The tuple after the split of string and number : ('Geeks', '4321')
    6=
    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']
    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']
    2221
    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__

    import re
    
    # Format is [(, ), ...]
    ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
           ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
          ('hello X42 I\'m a Y-32.35 string Z30',
           ['42', '-32.35', '30']),
          ('he33llo 42 I\'m a 32 string -30', 
           ['33', '42', '32', '-30']),
          ('h3110 23 cat 444.4 rabbit 11 2 dog', 
           ['3110', '23', '444.4', '11', '2']),
          ('hello 12 hi 89', 
           ['12', '89']),
          ('4', 
           ['4']),
          ('I like 74,600 commas not,500', 
           ['74,600', '500']),
          ('I like bad math 1+2=.001', 
           ['1', '+2', '.001'])]
    
    for s, r in ss:
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
        if rr == r:
            print('GOOD')
        else:
            print('WRONG', rr, 'should be', r)
    
    3=
    import re
    
    # Format is [(, ), ...]
    ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
           ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
          ('hello X42 I\'m a Y-32.35 string Z30',
           ['42', '-32.35', '30']),
          ('he33llo 42 I\'m a 32 string -30', 
           ['33', '42', '32', '-30']),
          ('h3110 23 cat 444.4 rabbit 11 2 dog', 
           ['3110', '23', '444.4', '11', '2']),
          ('hello 12 hi 89', 
           ['12', '89']),
          ('4', 
           ['4']),
          ('I like 74,600 commas not,500', 
           ['74,600', '500']),
          ('I like bad math 1+2=.001', 
           ['1', '+2', '.001'])]
    
    for s, r in ss:
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
        if rr == r:
            print('GOOD')
        else:
            print('WRONG', rr, 'should be', r)
    
    5

    The original string is : Geeks4321
    The tuple after the split of string and number : ['Geeks', '4321']


    Điều này còn hơn một chút, nhưng bạn có thể mở rộng biểu thức regex để tính toán ký hiệu khoa học quá.

    import re
    
    # Format is [(, ), ...]
    ss = [("apple-12.34 ba33na fanc-14.23e-2yapple+45e5+67.56E+3",
           ['-12.34', '33', '-14.23e-2', '+45e5', '+67.56E+3']),
          ('hello X42 I\'m a Y-32.35 string Z30',
           ['42', '-32.35', '30']),
          ('he33llo 42 I\'m a 32 string -30', 
           ['33', '42', '32', '-30']),
          ('h3110 23 cat 444.4 rabbit 11 2 dog', 
           ['3110', '23', '444.4', '11', '2']),
          ('hello 12 hi 89', 
           ['12', '89']),
          ('4', 
           ['4']),
          ('I like 74,600 commas not,500', 
           ['74,600', '500']),
          ('I like bad math 1+2=.001', 
           ['1', '+2', '.001'])]
    
    for s, r in ss:
        rr = re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", s)
        if rr == r:
            print('GOOD')
        else:
            print('WRONG', rr, 'should be', r)
    

    Cho tất cả tốt!

    Ngoài ra, bạn có thể nhìn vào Regex tích hợp keo AWS

    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 phân tách số và chữ cái 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 ..