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实现自动重启本程序的方法
Jul 09 Python
浅谈scrapy 的基本命令介绍
Jun 13 Python
python OpenCV学习笔记实现二维直方图
Feb 08 Python
Python socket实现简单聊天室
Apr 01 Python
django将图片上传数据库后在前端显式的方法
May 25 Python
Python的高阶函数用法实例分析
Apr 11 Python
十个Python练手的实战项目,学会这些Python就基本没问题了(推荐)
Apr 26 Python
Python虚拟环境的原理及使用详解
Jul 02 Python
python实现宿舍管理系统
Nov 22 Python
django admin后管定制-显示字段的实例
Mar 11 Python
python框架flask入门之路由及简单实现方法
Jun 07 Python
如何通过python计算圆周率PI
Nov 11 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遍历文件夹与子目录的函数代码
2011/09/26 PHP
php中mkdir函数用法实例分析
2014/11/15 PHP
在Debian系统下配置LNMP的教程
2015/07/09 PHP
thinkPHP下ueditor的使用方法详解
2015/12/26 PHP
向fckeditor编辑器插入指定代码的方法
2007/05/25 Javascript
JS 控制CSS样式表
2009/08/20 Javascript
Javascript 面向对象 继承
2010/05/13 Javascript
javascript for循环从入门到偏门(效率优化+奇特用法)
2012/08/01 Javascript
js实现跨域的4种实用方法原理分析
2015/10/29 Javascript
Angularjs 实现分页功能及示例代码
2016/09/14 Javascript
javascript使用闭包模拟对象的私有属性和方法
2016/10/05 Javascript
jQuery选择器中的特殊符号处理方法
2017/09/08 jQuery
JS解析后台返回的JSON格式数据实例
2018/08/06 Javascript
Flutter部件内部状态管理小结之实现Vue的v-model功能
2019/06/11 Javascript
ES6中new Function()语法及应用实例分析
2020/02/19 Javascript
Vue+ElementUI 中级联选择器Bug问题的解决
2020/07/31 Javascript
vue 导出文件,携带请求头token操作
2020/09/10 Javascript
微信小程序input抖动问题的修复方法
2021/03/03 Javascript
python快速查找算法应用实例
2014/09/26 Python
ubuntu系统下使用pm2设置nodejs开机自启动的方法
2018/05/12 NodeJs
python删除本地夹里重复文件的方法
2020/11/19 Python
Django REST Framework之频率限制的使用
2019/09/29 Python
python 生成任意形状的凸包图代码
2020/04/16 Python
基于SQLAlchemy实现操作MySQL并执行原生sql语句
2020/06/10 Python
简单了解Python变量作用域正确使用方法
2020/06/12 Python
Merrell迈乐澳大利亚网站:购买户外登山鞋
2017/05/28 全球购物
俄罗斯领先的移动和数字设备在线商店:Svyaznoy.ru
2020/12/21 全球购物
企业管理专业个人求职信范文
2013/09/24 职场文书
珍爱生命演讲稿
2014/05/10 职场文书
群众路线教育实践活动批评与自我批评
2014/09/15 职场文书
幼儿园感恩节活动方案
2014/10/06 职场文书
销售2014年度工作总结
2014/12/08 职场文书
个人催款函范文
2015/06/24 职场文书
运动会通讯稿200字
2015/07/20 职场文书
防溺水主题班会教案
2015/08/12 职场文书
MySQL 主从复制数据不一致的解决方法
2022/03/18 MySQL