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实现去除代码前行号的方法
Mar 10 Python
Python的函数的一些高阶特性
Apr 27 Python
python3实现TCP协议的简单服务器和客户端案例(分享)
Jun 14 Python
python实现自主查询实时天气
Jun 22 Python
python3 对list中每个元素进行处理的方法
Jun 29 Python
pandas dataframe添加表格框线输出的方法
Feb 08 Python
python实现kNN算法识别手写体数字的示例代码
Aug 16 Python
在Python中等距取出一个数组其中n个数的实现方式
Nov 27 Python
Python流程控制常用工具详解
Feb 24 Python
判断Threading.start新线程是否执行完毕的实例
May 02 Python
python使用for...else跳出双层嵌套循环的方法实例
May 17 Python
用Python将GIF动图分解成多张静态图片
Jun 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 实现进制相互转换
2016/04/07 PHP
java微信开发之上传下载多媒体文件
2016/06/24 PHP
Yii CGridView用法实例详解
2016/07/12 PHP
PHP使用观察者模式处理异常信息的方法详解
2019/09/24 PHP
JavaScript 题型问答有答案参考
2010/02/17 Javascript
JSP跨iframe如何传递参数实现代码
2013/09/21 Javascript
js实现的常用的左侧导航效果
2013/10/17 Javascript
javascript:void(0)的作用示例介绍
2013/10/28 Javascript
jQuery使用fadein方法实现渐出效果实例
2015/03/27 Javascript
jQuery实现带滚动导航效果的全屏滚动相册实例
2015/06/19 Javascript
javascript实现倒计时跳转页面
2016/01/17 Javascript
jQuery与JS加载事件用法分析
2016/09/04 Javascript
浅谈jquery选择器 :first与:first-child的区别
2016/11/20 Javascript
jQuery实现拖动剪裁图片作为头像
2016/12/28 Javascript
jquery 遍历hash操作示例【基于ajax交互】
2019/10/12 jQuery
Vue实现简单的跑马灯
2020/05/25 Javascript
jquery实现上传图片功能
2020/06/29 jQuery
[56:14]Fnatic vs OG 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
Python写的PHPMyAdmin暴力破解工具代码
2014/08/06 Python
在windows下Python打印彩色字体的方法
2018/05/15 Python
python opencv3实现人脸识别(windows)
2018/05/25 Python
Python检查和同步本地时间(北京时间)的实现方法
2018/12/03 Python
Python多线程应用于自动化测试操作示例
2018/12/06 Python
python实发邮件实例详解
2019/11/11 Python
解决python彩色螺旋线绘制引发的问题
2019/11/23 Python
Pytorch 数据加载与数据预处理方式
2019/12/31 Python
Python多线程threading join和守护线程setDeamon原理详解
2020/03/18 Python
Django中从mysql数据库中获取数据传到echarts方式
2020/04/07 Python
英国最大的网上药品商店:Chemist Direct
2017/12/16 全球购物
三星新西兰官网:Samsung新西兰
2019/03/05 全球购物
C#笔试题集合
2013/06/21 面试题
Java语言程序设计测试题判断题部分
2013/01/06 面试题
银行求职自荐书
2014/06/25 职场文书
专业见习报告范文
2014/11/03 职场文书
解决python存数据库速度太慢的问题
2021/04/23 Python
CSS3实现360度循环旋转功能
2022/02/12 HTML / CSS