检查字符串是否具有希伯来字符的正确方法

问题描述 投票:1回答:3

希伯来语在1424和1514之间(或0590到05EA的十六进制)具有unicode表示。

我正在寻找正确,最有效和最pythonic的方法来实现这一目标。

首先,我想到了这个:

for c in s:
    if ord(c) >= 1424 and ord(c) <= 1514:
        return True
return False

然后,我提供了一个更高级的实现:

return any(map(lambda c: (ord(c) >= 1424 and ord(c) <= 1514), s))

也许:

return any([(ord(c) >= 1424 and ord(c) <= 1514) for c in s])

其中哪些是最好的?还是我应该采取其他方式?

python ord
3个回答
16
投票

您可以做:

# Python 3.
return any("\u0590" <= c <= "\u05EA" for c in s)
# Python 2.
return any(u"\u0590" <= c <= u"\u05EA" for c in s)

1
投票

您的基本选项是:

  1. 与包含字符范围的正则表达式匹配;或
  2. 迭代字符串,测试包含所有目标字符的字符串或集合中字符的成员资格,如果找到匹配项,则中断。

仅实际测试可以显示哪个会更快。


1
投票

使用unidcodedata检查第一个字符很简单:

import unicodedata

def is_greek(term):
    return 'GREEK' in unicodedata.name(term.strip()[0])


def is_hebrew(term):
    return 'HEBREW' in unicodedata.name(term.strip()[0])
© www.soinside.com 2019 - 2024. All rights reserved.