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:
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ả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 ? True0____11
The original string is : Geeks forGeeks Does string contain spaces ? True2
The original string is : Geeks forGeeks Does string contain spaces ? True3
The original string is : Geeks forGeeks Does string contain spaces ? True4
The original string is : Geeks forGeeks Does string contain spaces ? True5
The original string is : Geeks forGeeks Does string contain spaces ? True6
The original string is : Geeks forGeeks Does string contain spaces ? True7
The original string is : Geeks forGeeks Does string contain spaces ? True8
The original string is : Geeks forGeeks
Does string contain spaces ? True
9The original string is : Geeks forGeeks
Does string contain spaces ? True
1 The original string is : Geeks forGeeks
Does string contain spaces ? True
1The 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
3The original string is : Geeks forGeeks
Does string contain spaces ? True
4The original string is : Geeks forGeeks
Does string contain spaces ? True
7 The original string is : Geeks forGeeks
Does string contain spaces ? True
6The 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 ? True0____11
The original string is : Geeks forGeeks Does string contain spaces ? True2
The original string is : Geeks forGeeks Does string contain spaces ? True3
The original string is : Geeks forGeeks Does string contain spaces ? True4
The original string is : Geeks forGeeks Does string contain spaces ? True5
The original string is : Geeks forGeeks Does string contain spaces ? True6
The original string is : Geeks forGeeks Does string contain spaces ? True7
The original string is : Geeks forGeeks Does string contain spaces ? True8
The original string is : Geeks forGeeks
Does string contain spaces ? True
9The original string is : Geeks forGeeks
Does string contain spaces ? True
1 The original string is : Geeks forGeeks
Does string contain spaces ? True
1The 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
3The original string is : Geeks forGeeks
Does string contain spaces ? True
4The original string is : Geeks forGeeks
Does string contain spaces ? True
7 The original string is : Geeks forGeeks
Does string contain spaces ? True
6The 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 ? True0____11
The original string is : Geeks forGeeks Does string contain spaces ? True9
The original string is : Geeks forGeeks Does string contain spaces ? True3
isidentifier
1isidentifier
2 The original string is : Geeks forGeeks Does string contain spaces ? True7
The original string is : Geeks forGeeks Does string contain spaces ? True8
isidentifier
5
The original string is : Geeks forGeeks Does string contain spaces ? True1
isidentifier
7Phươ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;
isidentifier
6isidentifier
5
The original string is : Geeks forGeeks Does string contain spaces ? True1
isidentifier
9The original string is : Geeks forGeeks Does string contain spaces ? True3
isidentifier
1in
2 The original string is : Geeks forGeeks Does string contain spaces ? True7
The original string is : Geeks forGeeks Does string contain spaces ? True6
The original string is : Geeks forGeeks Does string contain spaces ? True7
Đầ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 ? True0____11
The original string is : Geeks forGeeks Does string contain spaces ? True9
The original string is : Geeks forGeeks Does string contain spaces ? True3
isidentifier
1isidentifier
2 The original string is : Geeks forGeeks Does string contain spaces ? True7
The original string is : Geeks forGeeks Does string contain spaces ? True8
import
4
The original string is : Geeks forGeeks Does string contain spaces ? True1
import
6isidentifier
5
The original string is : Geeks forGeeks Does string contain spaces ? True1
isidentifier
7re
0 re
1in
re
3
isidentifier
6isidentifier
8re
6
re
7import
4
The original string is : Geeks forGeeks Does string contain spaces ? True7
The original string is : Geeks forGeeks Does string contain spaces ? True1
isidentifier
8
The original string is : Geeks forGeeks Does string contain spaces ? True03
The original string is : Geeks forGeeks Does string contain spaces ? True1
isidentifier
4isidentifier
5isidentifier
6isidentifier
5
The original string is : Geeks forGeeks Does string contain spaces ? True1
isidentifier
9The original string is : Geeks forGeeks Does string contain spaces ? True3
isidentifier
1in
2 The original string is : Geeks forGeeks Does string contain spaces ? True7
The original string is : Geeks forGeeks Does string contain spaces ? True6
The original string is : Geeks forGeeks Does string contain spaces ? True7
Đầu ra
The original string is : Geeks forGeeks Does string contain spaces ? True