当前位置: 首页 > 工具软件 > pinyin.py > 使用案例 >

python lazy_pinyin模块 isalpha函数

太叔超英
2023-12-01
"""
@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-&']

 

 类似资料: