Hướng dẫn how do you check if a string contains all spaces in python? - làm cách nào để kiểm tra xem chuỗi có chứa tất cả khoảng trắng trong python không?

Bạn đã đề cập đến khoảng trắng nói chung, thay vì chỉ là không gian. Tôi tình cờ thấy một giải pháp với isidentifier. Mỗi trường W3:

Show

    Một chuỗi được coi là một định danh hợp lệ nếu nó chỉ chứa các chữ cái chữ và chữ cái (A-Z) và (0-9) hoặc nhấn mạnh (_). Một định danh hợp lệ không thể bắt đầu với một số hoặc chứa bất kỳ không gian nào.

    Vì vậy, nếu điều này phù hợp với yêu cầu của bạn, Isidentifier nhanh chóng và dễ sử dụng.

    Ai đó đã đề cập đến hiệu quả của regex, và tôi đã tò mò:

    import timeit
    
    setup='import re; rs="\s"; rc=re.compile(rs); s="applebananacanteloupe"'
    stm1='re.search(rs,s)'
    stm2='re.search(rc,s)'
    stm3='" " in s'
    stm4='s.isidentifier()'
    
    timeit.repeat(stm1,setup)
    # result: [0.9235025509842671, 0.8889087940042373, 0.8771460619755089, 0.8753634429886006, 1.173506731982343]
    
    timeit.repeat(stm2,setup)
    # results: [1.160843407997163, 1.1500899779784959, 1.1857644470001105, 1.1485740720236208, 1.2856045850203373]
    # compiled slower than uncompiled? Hmm, I don't get regex...
    
    timeit.repeat(stm3,setup)
    # [0.039073383988579735, 0.03403249100665562, 0.03481135700712912, 0.034628107998287305, 0.03392893000273034]
    
    timeit.repeat(stm4,setup)
    # [0.08866660299827345, 0.09206177099258639, 0.08418851799797267, 0.08478381999884732, 0.09471498697530478]
    
    

    Vì vậy, isidentifier gần như nhanh như in và nhanh hơn 10 lần so với Regex. Lưu ý rằng về mặt kỹ thuật, không có gì đảm bảo rằng ý tưởng của Python về những gì một định danh sẽ không thay đổi - nhưng cũng có khả năng nếu có, mã của bạn sẽ cần một số việc làm lại.

    Cải thiện bài viết

    Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    ĐọcMethod #1 : Using regex This kind of problem can be solved using the regex utility offered by python. By feeding the appropriate regex string in search(), we can check presence of space in a string. 

    Python3

    Bàn luận

    Đôi khi, chúng ta có thể gặp vấn đề trong đó chúng ta cần kiểm tra xem chuỗi có bất kỳ khoảng trống nào không. Loại vấn đề này có thể nằm trong miền học máy để có được loại dữ liệu cụ thể. Hãy để thảo luận về những cách nhất định trong đó loại vấn đề này có thể được giải quyết. Phương pháp số 1: Sử dụng Regex, loại vấn đề này có thể được giải quyết bằng cách sử dụng tiện ích regex do Python cung cấp. Bằng cách cung cấp chuỗi regex thích hợp trong search (), chúng ta có thể kiểm tra sự hiện diện của không gian trong một chuỗi. & Nbsp;

    import re

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    0____11
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    2

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    3
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    4
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    5
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    6
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    7
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    8

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    9
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    1
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    1
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    2

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    3
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    4
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    7
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    6
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    7
    This task can also be performed using in operator. Just required to check for a space in the string. The verdict returned is true even if a single space is found and false otherwise. 

    Python3

    Đôi khi, chúng ta có thể gặp vấn đề trong đó chúng ta cần kiểm tra xem chuỗi có bất kỳ khoảng trống nào không. Loại vấn đề này có thể nằm trong miền học máy để có được loại dữ liệu cụ thể. Hãy để thảo luận về những cách nhất định trong đó loại vấn đề này có thể được giải quyết. Phương pháp số 1: Sử dụng Regex, loại vấn đề này có thể được giải quyết bằng cách sử dụng tiện ích regex do Python cung cấp. Bằng cách cung cấp chuỗi regex thích hợp trong search (), chúng ta có thể kiểm tra sự hiện diện của không gian trong một chuỗi. & Nbsp;

    import re

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    0____11
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    2

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    3
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    4
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    5
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    6
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    7
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    8

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    9
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    1
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    1
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    2

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    3
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    4
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    7
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    6
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    7
    .

    Đầu ra: & nbsp;

    Python3

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    0____11
    The original string is : Geeks forGeeks
    Does string contain spaces ? True
    9

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    3isidentifier1isidentifier2
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    7
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    8

    isidentifier5

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    1isidentifier7

    Phương pháp số 2: Sử dụng toán tử trong toán tử, tác vụ này cũng có thể được thực hiện bằng toán tử. Chỉ cần kiểm tra một không gian trong chuỗi. Phán quyết được trả lại là đúng ngay cả khi một không gian duy nhất được tìm thấy và sai nếu không. & Nbsp;

    isidentifier6isidentifier5

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    1isidentifier9

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    3isidentifier1in2
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    7
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    6
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    7

    Đầu ra

    The original string is : Geeks forGeeks
    Does string contain spaces ? True

    Phương pháp số 4: Sử dụng phương thức ISSPACE () & nbsp;
     

    Python3

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    0____11
    The original string is : Geeks forGeeks
    Does string contain spaces ? True
    9

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    3isidentifier1isidentifier2
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    7
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    8

    import4

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    1import6

    isidentifier5

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    1isidentifier7

    re0 re1in re3

    isidentifier6isidentifier8re6

    re7import4

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    7
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    1

    isidentifier8

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    03
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    1isidentifier4isidentifier5

    isidentifier6isidentifier5

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    1isidentifier9

    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    3isidentifier1in2
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    7
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    6
    The original string is : Geeks  forGeeks
    Does string contain spaces ? True
    7

    Đầu ra

    The original string is : Geeks forGeeks
    Does string contain spaces ? True