python lazy_pinyin模块 isalpha函数
2023-09-27 14:29:08 时间
"""
@time: 2020/7/30 12:01
@file: csdn_temp_01.py
@desc: python lazy_pinyin模块 isalpha函数
"""
# Python 中拼音库 PyPinyin 的用法
# https://blog.csdn.net/zhoulei124/article/details/89055403
from pypinyin import lazy_pinyin
def temp_test(chars):
"""
test 将汉字转换为汉语拼音
:return:
"""
# chars_pinyin = list()
# for char in chars:
# lazy_pinyin_char = lazy_pinyin(char)
# # print('--- char = {} lazy_pinyin_char = {}'.format(char, lazy_pinyin_char))
# chars_pinyin.append(lazy_pinyin_char)
# return chars_pinyin
return [lazy_pinyin(char) for char in chars]
def chars_sort_by_first_pinyin(chars):
"""
汉字按拼音首字母排序
:param chars:
:return:
"""
chars.sort(key=lambda char: lazy_pinyin(char)[0][0])
def is_alpha(char):
"""判断字符串包含字符是否全部是字母"""
return char.isalpha() # 判断是否是字母,返回True OR False(汉字也返回True,汉字是字母?)
def check_is_alpha(char_list):
result_dict = dict()
for char in char_list:
result_dict.update({
char: is_alpha(char)
})
return result_dict
def test():
"""
测试 - 汉字/英文字母/数字/特殊符号转pinyin测试 - 汉字返回汉字拼音/英文和数字以及特殊字符原样返回
:return:
"""
print('--- = {}'.format(lazy_pinyin('国家')))
print('--- = {}'.format(lazy_pinyin('hehe')))
print('--- = {}'.format(lazy_pinyin('-hehe')))
print('--- = {}'.format(lazy_pinyin('_hehe')))
print('--- = {}'.format(lazy_pinyin('0-hehe')))
print('--- = {}'.format(lazy_pinyin('&-V-&')))
if __name__ == "__main__":
chars = ['鑫', '鹭', '榕', '柘', '珈', '骅', '孚', '迦', '瀚', '濮', '浔', '沱', '泸', '恺', '怡', '岷', '萃', '兖']
# 汉字转换为拼音
chars_pinyin = temp_test(chars)
print('--- chars_pinyin = {}'.format(chars_pinyin))
# 汉字按拼音首字母进行排序 - 列表是可变数据类型,直接在原列表上进行排序
chars_sort_by_first_pinyin(chars)
print('--- chars_sort = {}'.format(chars))
# 判断字符是否时字母
char_list = ["国", "家", "hehe", "HEHE", "--", "__", ":::", "&&&", "&-V-&", ",", "。", "&-V-&", "0", "1"]
res1 = check_is_alpha(char_list)
print('--- res1 = {}'.format(res1))
# 汉字/英文字母/数字/特殊符号转pinyin测试
test()
输出:
--- chars_pinyin = [['xin'], ['lu'], ['rong'], ['zhe'], ['jia'], ['hua'], ['fu'], ['jia'], ['han'], ['pu'], ['xun'], ['tuo'], ['lu'], ['kai'], ['yi'], ['min'], ['cui'], ['yan']]
--- chars_sort = ['萃', '孚', '骅', '瀚', '珈', '迦', '恺', '鹭', '泸', '岷', '濮', '榕', '沱', '鑫', '浔', '怡', '兖', '柘']
--- res1 = {'国': True, '家': True, 'hehe': True, 'HEHE': True, '--': False, '__': False, ':::': False, '&&&': False, '&-V-&': False, ',': False, '。': False, '0': False, '1': False}
--- = ['guo', 'jia']
--- = ['hehe']
--- = ['-hehe']
--- = ['_hehe']
--- = ['0-hehe']
--- = ['&-V-&']
相关文章
- Python常用函数:os模块【操作文件:remove、rename、stat、listdir】【操作目录:path、mkdir、rmdir、getcwd、chdir、walk】
- Python之CVXOPT模块
- 通过python xlsxwriter模块生成EXCEL柱状图、饼图
- 【Python基础】函数基础 || 模块的导入 || 函数定义和使用 || PyCharm中的调试F7和F8 || Ctrl+Q快速查看文档注释 || 函数的返回值 || 函数的嵌套调用打印分割线
- Python模块学习 - Paramiko
- Python sys 模块
- Python学习笔记 day9 进程与线程
- PyQt(Python+Qt)学习随笔:QAbstractItemView的alternatingRowColors属性
- 第12.6节 Python标准库其他内置模块导览
- 第11.23节 Python 中re模块的搜索替换功能:sub及subn函数
- 第11.22节 Python 中re模块的字符串分割器:split函数
- 第6章 Python中的动态可执行方法目录
- Python的定时器
- Python pandas.DataFrame.iterrows函数方法的使用
- python的os模块学习笔记
- Python基础:第一个Python程序(2)
- Python的re模块 --- 正则表达式操作
- python - # 此函数将指定文件移动到指定目录(os模块和shutil模块)
- Python使用psutil模块获取CPU,内存以及硬盘信息
- python发邮件