希伯来语在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 3.
return any("\u0590" <= c <= "\u05EA" for c in s)
# Python 2.
return any(u"\u0590" <= c <= u"\u05EA" for c in s)
您的基本选项是:
仅实际测试可以显示哪个会更快。
使用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])