简体   繁体   English

将 isalpha() 和 isspace() 合并为 1 个语句

[英]Combine isalpha() and isspace() into 1 statement

I am looking for a function that combines the methods isalpha() and isspace() into a single method.我正在寻找一个将方法isalpha()isspace()成一个方法的函数。 I want to check if a given string only contains letters and/or spaces, for example:我想检查给定的字符串是否只包含字母和/或空格,例如:

"This is text".isalpha_or_space()
# True

However, with the 2 methods, I get:但是,使用这两种方法,我得到:

"This is text".isalpha() or "This is text".isspace()
# False

as the string is not only alpha nor space.因为字符串不仅是 alpha 也不是空格。


Of course, I could iterate over every character and check it for space or alpha.当然,我可以遍历每个字符并检查它的空格或字母。

I could also compare the string with ("abcdefghijklmnopqrstuvwxyz" + " ")我还可以将字符串与("abcdefghijklmnopqrstuvwxyz" + " ")

However, both of these approaches don't seem very pythonic to me - convince me otherwise.然而,这两种方法对我来说似乎都不是很 Pythonic - 否则说服我。

The most Pythonic will be to use a def for this:最 Pythonic 的做法是为此使用def

def isalpha_or_space(self):
    if self == "":
        return False
    for char in self:
        if not (char.isalpha() or char.isspace()):
            return False
    return True

It is not easy to contribute this as a method on str , since Python does not encourage the monkeypatching of built-in types.将其作为str上的一个方法进行贡献并不容易,因为 Python 不鼓励对内置类型进行monkeypatching。 My recommendation is just to leave this as a module level function.我的建议只是将其保留为模块级功能。

Nonetheless, it is still possible to mimic the interface of a method, since most namespaces in Python are writable if you know where to find them.尽管如此,仍然可以模拟方法的接口,因为 Python 中的大多数名称空间如果您知道在哪里可以找到它们,那么它们都是可写的。 The suggestion below is not Pythonic, and relies on implementation detail.下面的建议不是 Pythonic,而是依赖于实现细节。

>>> import gc
>>> def monkeypatch(type_, func): 
...     gc.get_referents(type_.__dict__)[0][func.__name__] = func 
...
>>> monkeypatch(str, isalpha_or_space)
>>> "hello world".isalpha_or_space()
True

Use a regular expression (regex) :使用正则表达式(regex)

>>> import re
>>> result = re.match('[a-zA-Z\s]+$', "This is text")
>>> bool(result)
True

Breakdown:分解:

  • re - Python's regex module re - Python 的正则表达式模块
  • [a-zA-Z\\s] - Any letter or whitespace [a-zA-Z\\s] - 任何字母或空格
  • + - One or more of the previous item + - 一项或多项前一项
  • $ - End of string $ - 字符串结束

The above works with ASCII letters.以上适用于 ASCII 字母。 For the full Unicode range on Python 3, unfortunately the regex is a bit complicated:对于 Python 3 上的完整 Unicode 范围,不幸的是正则表达式有点复杂:

>>> result = re.match('([^\W\d_]|\s)+$', 'un café')

Breakdown:分解:

  • (x|y) - x or y (x|y) - xy
  • [^\\W\\d_] - Any word character except a number or an underscore [^\\W\\d_] - 除数字或下划线外的任何单词字符

From Mark Tolonen 's answer on How to match all unicode alphabetic characters and spaces in a regex?来自Mark Tolonen关于如何匹配正则表达式中的所有 unicode 字母字符和空格回答

您可以使用以下解决方案:

s != '' and all(c.isalpha() or c.isspace() for c in s)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM