Hướng dẫn how to find number in string python - cách tìm số trong chuỗi python

Đ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

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
    This problem can be solved by using split function to convert string to list and then the list comprehension which can help us iterating through the list and isdigit function helps to get the digit out of a string.

    Nhiều lần, trong khi làm việc với các chuỗi, chúng tôi bắt gặp vấn đề này, trong đó chúng tôi cần phải có tất cả các lần xuất hiện bằng số. Loại vấn đề này thường xảy ra trong lập trình cạnh tranh và cũng trong phát triển web. Hãy để thảo luận về những cách nhất định trong đó vấn đề này có thể được giải quyết.

    Phương pháp số 1: Sử dụng danh sách hiểu + isdigit() +

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    0 Vấn đề này có thể được giải quyết bằng cách sử dụng chức năng phân chia để chuyển đổi chuỗi thành danh sách và sau đó danh sách hiểu có thể giúp chúng tôi lặp đi lặp lại trong danh sách và chức năng ISDigit giúp lấy chữ số ra khỏi chuỗi.

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    1
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    2
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    3

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    4
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    5
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    6
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    7
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    8

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    9
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    2
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    1
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    2223__

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    4
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    5
    inp_str = "Python4Journaldev"
    
    print("Original String : " + inp_str) 
    num = ""
    for c in inp_str:
        if c.isdigit():
            num = num + c
    print("Extracted numbers from the list : " + num) 
    
    
    2
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    7
    inp_str = "Python4Journaldev"
    
    print("Original String : " + inp_str) 
    num = ""
    for c in inp_str:
        if c.isdigit():
            num = num + c
    print("Extracted numbers from the list : " + num) 
    
    
    4
    inp_str = "Python4Journaldev"
    
    print("Original String : " + inp_str) 
    num = ""
    for c in inp_str:
        if c.isdigit():
            num = num + c
    print("Extracted numbers from the list : " + num) 
    
    
    5

    This particular problem can also be solved using python regex, we can use the findall function to check for the numeric occurrences using matching regex string.

    Đầu ra:

    Nhiều lần, trong khi làm việc với các chuỗi, chúng tôi bắt gặp vấn đề này, trong đó chúng tôi cần phải có tất cả các lần xuất hiện bằng số. Loại vấn đề này thường xảy ra trong lập trình cạnh tranh và cũng trong phát triển web. Hãy để thảo luận về những cách nhất định trong đó vấn đề này có thể được giải quyết.

    Phương pháp số 1: Sử dụng danh sách hiểu + isdigit() +

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    0 Vấn đề này có thể được giải quyết bằng cách sử dụng chức năng phân chia để chuyển đổi chuỗi thành danh sách và sau đó danh sách hiểu có thể giúp chúng tôi lặp đi lặp lại trong danh sách và chức năng ISDigit giúp lấy chữ số ra khỏi chuỗi.

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    1
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    2
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    3

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    4
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    5
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    6
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    7
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    8

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    4
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    5
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    6
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    7
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    8

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    9
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    2
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    1
    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    
    2223__

    The original string : There are 2 apples for 4 persons
    The numbers list is : [2, 4]
    


    Xin chào, Độc giả! Trong bài viết này, chúng tôi sẽ tập trung vào các cách để trích xuất các chữ số từ chuỗi Python. Vì vậy, hãy để chúng tôi bắt đầu.ways to extract digits from a Python String. So, let us get started.


    1. Sử dụng hàm isDigit () để trích xuất các chữ số từ chuỗi python

    Python cung cấp cho chúng tôi

    Original string : Hey readers, we all are here be 4 the time!
    The numbers list is : [4]
    
    6 để kiểm tra sự hiện diện của các chữ số trong một chuỗi.

    Hàm python isDigit () trả về true nếu chuỗi đầu vào chứa các ký tự chữ số trong đó.True if the input string contains digit characters in it.

    Syntax::

    Chúng tôi không cần chuyển bất kỳ tham số nào cho nó. Là một đầu ra, nó trả về đúng hoặc sai tùy thuộc vào sự hiện diện của các ký tự chữ số trong một chuỗi.

    Ví dụ 1:

    inp_str = "Python4Journaldev"
    
    print("Original String : " + inp_str) 
    num = ""
    for c in inp_str:
        if c.isdigit():
            num = num + c
    print("Extracted numbers from the list : " + num) 
    
    

    Trong ví dụ này, chúng tôi đã lặp lại ký tự chuỗi đầu vào bằng ký tự bằng cách sử dụng một vòng lặp. Ngay khi hàm isDigit () gặp phải một chữ số, nó sẽ lưu trữ nó thành một biến chuỗi có tên ‘num.

    Do đó, chúng ta thấy đầu ra như được hiển thị bên dưới

    Output:

    Original String : Python4Journaldev
    Extracted numbers from the list : 4
    

    Bây giờ, chúng ta thậm chí có thể sử dụng khả năng hiểu danh sách Python để câu lạc bộ chức năng lặp và idigit () thành một dòng.

    Bằng cách này, các ký tự chữ số được lưu trữ vào danh sách ‘Num, như được hiển thị bên dưới:

    Ví dụ 2:

    inp_str = "Hey readers, we all are here be 4 the time!"
    
    
    print("Original string : " + inp_str) 
    
    
    num = [int(x) for x in inp_str.split() if x.isdigit()] 
    
     
    print("The numbers list is : " + str(num)) 
    
    

    Output:

    Original string : Hey readers, we all are here be 4 the time!
    The numbers list is : [4]
    


    2. Sử dụng thư viện regex để trích xuất các chữ số

    Thư viện biểu thức chính quy Python có tên ‘Thư viện Regex‘ cho phép chúng tôi phát hiện sự hiện diện của các ký tự cụ thể như chữ số, một số ký tự đặc biệt, v.v. từ một chuỗi.regex library‘ enables us to detect the presence of particular characters such as digits, some special characters, etc. from a string.

    Chúng ta cần nhập thư viện Regex vào môi trường Python trước khi thực hiện bất kỳ bước nào nữa.

    Hơn nữa, chúng tôi

    Original string : Hey readers, we all are here be 4 the time!
    The numbers list is : [4]
    
    7 để trích xuất các ký tự chữ số từ chuỗi.Phần ‘\ d+, sẽ giúp hàm findall () để phát hiện sự hiện diện của bất kỳ chữ số nào.‘\d+’ would help the findall() function to detect the presence of any digit.

    Example:

    import re
    inp_str = "Hey readers, we all are here be 4 the time 1!"
    
    
    print("Original string : " + inp_str) 
    
    num = re.findall(r'\d+', inp_str) 
    
    print(num)
    
    

    Vì vậy, như đã thấy dưới đây, chúng tôi sẽ nhận được một danh sách tất cả các ký tự chữ số từ chuỗi.

    Output:

    Original string : Hey readers, we all are here be 4 the time 1!
    ['4', '1']
    


    Sự kết luận

    Bằng cách này, chúng tôi đã đi đến cuối chủ đề này.Hãy bình luận bên dưới, trong trường hợp bạn bắt gặp bất kỳ câu hỏi.

    Tôi khuyên tất cả các bạn nên thử triển khai các ví dụ trên bằng các cấu trúc dữ liệu như danh sách, dict, v.v.

    Để biết thêm các bài viết như vậy liên quan đến Python, hãy theo dõi và cho đến lúc đó, học hỏi hạnh phúc !!🙂