python获取一组汉字拼音首字母的方法


Posted in Python onJuly 01, 2015

本文实例讲述了python获取一组汉字拼音首字母的方法。分享给大家供大家参考。具体实现方法如下:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
def multi_get_letter(str_input): 
  if isinstance(str_input, unicode): 
    unicode_str = str_input 
  else: 
    try: 
      unicode_str = str_input.decode('utf8') 
    except: 
      try: 
        unicode_str = str_input.decode('gbk') 
      except: 
        print 'unknown coding' 
        return 
  return_list = [] 
  for one_unicode in unicode_str: 
    return_list.append(single_get_first(one_unicode)) 
  return return_list 
def single_get_first(unicode1): 
  str1 = unicode1.encode('gbk') 
  try:     
    ord(str1) 
    return str1 
  except: 
    asc = ord(str1[0]) * 256 + ord(str1[1]) - 65536 
    if asc >= -20319 and asc <= -20284: 
      return 'a' 
    if asc >= -20283 and asc <= -19776: 
      return 'b' 
    if asc >= -19775 and asc <= -19219: 
      return 'c' 
    if asc >= -19218 and asc <= -18711: 
      return 'd' 
    if asc >= -18710 and asc <= -18527: 
      return 'e' 
    if asc >= -18526 and asc <= -18240: 
      return 'f' 
    if asc >= -18239 and asc <= -17923: 
      return 'g' 
    if asc >= -17922 and asc <= -17418: 
      return 'h' 
    if asc >= -17417 and asc <= -16475: 
      return 'j' 
    if asc >= -16474 and asc <= -16213: 
      return 'k' 
    if asc >= -16212 and asc <= -15641: 
      return 'l' 
    if asc >= -15640 and asc <= -15166: 
      return 'm' 
    if asc >= -15165 and asc <= -14923: 
      return 'n' 
    if asc >= -14922 and asc <= -14915: 
      return 'o' 
    if asc >= -14914 and asc <= -14631: 
      return 'p' 
    if asc >= -14630 and asc <= -14150: 
      return 'q' 
    if asc >= -14149 and asc <= -14091: 
      return 'r' 
    if asc >= -14090 and asc <= -13119: 
      return 's' 
    if asc >= -13118 and asc <= -12839: 
      return 't' 
    if asc >= -12838 and asc <= -12557: 
      return 'w' 
    if asc >= -12556 and asc <= -11848: 
      return 'x' 
    if asc >= -11847 and asc <= -11056: 
      return 'y' 
    if asc >= -11055 and asc <= -10247: 
      return 'z' 
    return '' 
def main(str_input): 
  a = multi_get_letter(str_input) 
  b = '' 
  for i in a: 
    b= b+i 
  print b 
if __name__ == "__main__": 
  str_input=u'欢迎你' 
  main(str_input)

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
python学习数据结构实例代码
May 11 Python
Python2.x与Python3.x的区别
Jan 14 Python
Tensorflow之构建自己的图片数据集TFrecords的方法
Feb 07 Python
PyQt4实现下拉菜单可供选择并打印出来
Apr 20 Python
python二维码操作:对QRCode和MyQR入门详解
Jun 24 Python
python射线法判断检测点是否位于区域外接矩形内
Jun 28 Python
在Python中利用pickle保存变量的实例
Dec 30 Python
Django重设Admin密码过程解析
Feb 10 Python
Python virtualenv虚拟环境实现过程解析
Apr 18 Python
Python描述数据结构学习之哈夫曼树篇
Sep 07 Python
Windows安装Anaconda3的方法及使用过程详解
Jun 11 Python
Python中的tkinter库简单案例详解
Jan 22 Python
python的keyword模块用法实例分析
Jun 30 #Python
Python实现监控程序执行时间并将其写入日志的方法
Jun 30 #Python
python实现爬取千万淘宝商品的方法
Jun 30 #Python
python简单判断序列是否为空的方法
Jun 30 #Python
python检查序列seq是否含有aset中项的方法
Jun 30 #Python
python判断一个集合是否包含了另外一个集合中所有项的方法
Jun 30 #Python
python过滤字符串中不属于指定集合中字符的类实例
Jun 30 #Python
You might like
PHP查询网站的PR值
2013/10/30 PHP
PHP程序员必须清楚的问题汇总
2014/12/18 PHP
phplot生成图片类用法详解
2015/01/06 PHP
javascript Ext JS 状态默认存储时间
2009/02/15 Javascript
ExtJS扩展 垂直tabLayout实现代码
2009/06/21 Javascript
广泛收集的jQuery拖放插件集合
2012/04/09 Javascript
js中精确计算加法和减法示例
2014/03/28 Javascript
JavaScript操作DOM元素的childNodes和children区别
2015/04/01 Javascript
javascript下使用Promise封装FileReader
2016/02/19 Javascript
从源码看angular/material2 中 dialog模块的实现方法
2017/10/18 Javascript
vue-cli webpack 引入jquery的方法
2018/01/10 jQuery
微信小程序的授权实现过程解析
2019/08/02 Javascript
vue中axios的二次封装实例讲解
2019/10/14 Javascript
JavaScript实现省市区三级联动
2020/02/13 Javascript
[00:10]DOTA2全国高校联赛速递
2018/05/30 DOTA
Python获取Windows或Linux主机名称通用函数分享
2014/11/22 Python
用Python展示动态规则法用以解决重叠子问题的示例
2015/04/02 Python
Python中elasticsearch插入和更新数据的实现方法
2018/04/01 Python
python对离散变量的one-hot编码方法
2018/07/11 Python
Python中常用的内置方法
2019/01/28 Python
Python 多线程搜索txt文件的内容,并写入搜到的内容(Lock)方法
2019/08/23 Python
基于Python执行dos命令并获取输出的结果
2019/12/30 Python
Django CSRF认证的几种解决方案
2020/03/03 Python
Canvas与Image互相转换示例代码
2013/08/09 HTML / CSS
详解Html5 监听拦截Android返回键方法
2018/04/18 HTML / CSS
小米旗下精品生活电商平台:小米有品
2018/12/18 全球购物
时尚、社区、科技:SEVENSTORE
2019/04/26 全球购物
说出ArrayList,Vector, LinkedList的存储性能和特性
2015/01/04 面试题
工作交流会欢迎词
2014/01/12 职场文书
大学生毕业自我鉴定范文
2014/02/03 职场文书
省级优秀毕业生主要事迹
2014/05/29 职场文书
图书馆志愿者活动总结
2014/06/27 职场文书
思想作风纪律整顿心得体会
2014/09/04 职场文书
英雄儿女观后感
2015/06/09 职场文书
自荐信范文
2019/05/20 职场文书
win10更新失败无限重启解决方法
2022/04/19 数码科技