简体   繁体   English

如何检查字符串是否包含字母表中的任何字母?

[英]How can I check if a string contains ANY letters from the alphabet?

What is best pure Python implementation to check if a string contains ANY letters from the alphabet?检查字符串是否包含字母表中的任何字母的最佳纯 Python 实现是什么?

string_1 = "(555).555-5555"
string_2 = "(555) 555 - 5555 ext. 5555

Where string_1 would return False for having no letters of the alphabet in it and string_2 would return True for having letter.其中string_1将返回False以表示其中没有字母,而string_2将返回True以表示其中包含字母。

正则表达式应该是一种快速的方法:

re.search('[a-zA-Z]', the_string)

How about:怎么样:

>>> string_1 = "(555).555-5555"
>>> string_2 = "(555) 555 - 5555 ext. 5555"
>>> any(c.isalpha() for c in string_1)
False
>>> any(c.isalpha() for c in string_2)
True

You can use islower() on your string to see if it contains some lowercase letters (amongst other characters).您可以在字符串上使用islower()来查看它是否包含一些小写字母(以及其他字符)。 or it with isupper() to also check if contains some uppercase letters: or它与isupper()一起检查是否包含一些大写字母:

below: letters in the string: test yields true下面:字符串中的字母:测试结果为真

>>> z = "(555) 555 - 5555 ext. 5555"
>>> z.isupper() or z.islower()
True

below: no letters in the string: test yields false.下面:字符串中没有字母:测试产生错误。

>>> z= "(555).555-5555"
>>> z.isupper() or z.islower()
False
>>> 

Not to be mixed up with isalpha() which returns True only if all characters are letters, which isn't what you want.不要与isalpha()混淆,它仅在所有字符都是字母时才返回True ,这不是您想要的。

Note that Barm's answer completes mine nicely, since mine doesn't handle the mixed case well.请注意,巴姆的回答很好地完成了我回答,因为我回答不能很好地处理混合情况。

I liked the answer provided by @jean-françois-fabre , but it is incomplete.我喜欢@jean-françois-fabre提供的答案,但它不完整。
His approach will work, but only if the text contains purely lower- or uppercase letters:他的方法可行,但前提是文本包含纯小写或大写字母:

>>> text = "(555).555-5555 extA. 5555"
>>> text.islower()
False
>>> text.isupper()
False

The better approach is to first upper- or lowercase your string and then check.更好的方法是先将字符串大写或小写,然后再检查。

>>> string1 = "(555).555-5555 extA. 5555"
>>> string2 = '555 (234) - 123.32   21'

>>> string1.upper().isupper()
True
>>> string2.upper().isupper()
False

You can use regular expression like this:您可以像这样使用正则表达式:

import re

print re.search('[a-zA-Z]+',string)

I tested each of the above methods for finding if any alphabets are contained in a given string and found out average processing time per string on a standard computer.我测试了上述每种方法,以查找给定字符串中是否包含任何字母表,并找出标准计算机上每个字符串的平均处理时间。

~250 ns for约 250 纳秒

import re

~3 µs for约 3 µs

re.search('[a-zA-Z]', string)

~6 µs for约 6 微秒

any(c.isalpha() for c in string)

~850 ns for约 850 纳秒

string.upper().isupper()


Opposite to as alleged, importing re takes negligible time, and searching with re takes just about half time as compared to iterating isalpha() even for a relatively small string.与所声称的相反,导入re花费的时间可以忽略不计,与迭代isalpha()相比,即使对于相对较小的字符串,使用re搜索也只花费大约一半的时间
Hence for larger strings and greater counts, re would be significantly more efficient.因此,对于更大的字符串和更大的计数, re 会更有效。

But converting string to a case and checking case (ie any of upper().isupper() or lower().islower() ) wins here.但是将字符串转换为大小写并检查大小写(即upper().isupper()lower().islower() 中的任何一个)在这里获胜 In every loop it is significantly faster than re.search() and it doesn't even require any additional imports.在每个循环中,它都比re.search()快得多,甚至不需要任何额外的导入。

You can also do this in addition你也可以这样做

import re
string='24234ww'
val = re.search('[a-zA-Z]+',string) 
val[0].isalpha() # returns True if the variable is an alphabet
print(val[0]) # this will print the first instance of the matching value

Also note that if variable val returns None.另请注意,如果变量val返回 None。 That means the search did not find a match这意味着搜索没有找到匹配项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查字符串是否包含python中的所有字母? - How do I check if a string contains ALL letters of the alphabet in python? 如何检查字符串是否仅包含大写或小写字母? - How can I check if a string only contains uppercase or lowercase letters? 从字母表中删除字母时如何将点保留在字符串中 - How can i keep the dot in a string while removing letters from the alphabet 如何检查字符串是否包含字母? - How to check if a string contains letters? 如何检查 Python unicode 字符串是否包含非西方字母? - How can I check if a Python unicode string contains non-Western letters? 如何检查字符串是否只包含字母? - How to check if a string only contains letters? 如何检查字符串是否包含字母或字母和撇号但是没有数字? - How to check if a string contains letters or letters and an apostrophe but NO numbers? 我如何将 map 字母表作为键并将字母的出现作为值 - How can I map alphabet as keys and the occurence of the letters as values 循环遍历包含字母的字符串中的字母,以确定字典中缺少哪些字母 - Loop over letters in a string that contains the alphabet to determine which are missing from a dictionary 如何从字母选择中返回字母表中最早的字母? - How do I return the earliest letter in the alphabet from a choice of letters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM