gpt4 book ai didi

python - 相当于 python 2.x 中 unicode 字符串的 string.ascii_letters?

转载 作者:太空狗 更新时间:2023-10-29 17:44:32 26 4
gpt4 key购买 nike

在标准库的“string”模块中,

string.ascii_letters ## Same as string.ascii_lowercase + string.ascii_uppercase

'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

是否有一个类似的常量可以包含在 unicode 中被认为是字母的所有内容?

最佳答案

您可以构造自己的 Unicode 大写和小写字母常量:

import unicodedata as ud
all_unicode = ''.join(unichr(i) for i in xrange(65536))
unicode_letters = ''.join(c for c in all_unicode
if ud.category(c)=='Lu' or ud.category(c)=='Ll')

这使得字符串长度为 2153 个字符(窄 Unicode Python 构建)。对于像 letter in unicode_letters 这样的代码,使用集合会更快:

unicode_letters = set(unicode_letters)

关于python - 相当于 python 2.x 中 unicode 字符串的 string.ascii_letters?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2126551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com