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在Windows8下获取本机ip地址的方法
Mar 14 Python
pygame实现弹力球及其变速效果
Jul 03 Python
浅谈Python中的私有变量
Feb 28 Python
浅述python2与python3的简单区别
Sep 19 Python
Python多进程入门、分布式进程数据共享实例详解
Jun 03 Python
python,Django实现的淘宝客登录功能示例
Jun 12 Python
Python_查看sqlite3表结构,查询语句的示例代码
Jul 17 Python
python+tifffile之tiff文件读写方式
Jan 13 Python
python实现根据给定坐标点生成多边形mask的例子
Feb 18 Python
python如何操作mysql
Aug 17 Python
Django路由层URLconf作用及原理解析
Sep 24 Python
python 删除系统中的文件(按时间,大小,扩展名)
Nov 19 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
人工智能开始玩《星际争霸2》 你的操作跟得上吗?
2017/08/11 星际争霸
Terran历史背景
2020/03/14 星际争霸
建立文件交换功能的脚本(三)
2006/10/09 PHP
探讨方法的重写(覆载)详解
2013/06/08 PHP
PHP学习笔记(一) 简单了解PHP
2014/08/04 PHP
php从memcache读取数据再批量写入mysql的方法
2014/12/29 PHP
弹出模态框modal的实现方法及实例
2017/09/19 PHP
JavaScript中伪协议 javascript:使用探讨
2014/07/18 Javascript
node.js中的path.resolve方法使用说明
2014/12/08 Javascript
深入理解Vue-cli搭建项目后的目录结构探秘
2017/07/13 Javascript
jQuery访问浏览器本地存储cookie、localStorage和sessionStorage的基本用法
2017/10/20 jQuery
基于Bootstrap和JQuery实现动态打开和关闭tab页的实例代码
2019/06/10 jQuery
如何在微信小程序中实现Mixins方案
2019/06/20 Javascript
vue 对axios get pust put delete封装的实例代码
2020/01/05 Javascript
JavaScript 获取滚动条位置并将页面滑动到锚点
2021/02/08 Javascript
对numpy 数组和矩阵的乘法的进一步理解
2018/04/04 Python
Windows下anaconda安装第三方包的方法小结(tensorflow、gensim为例)
2018/04/05 Python
python之cv2与图像的载入、显示和保存实例
2018/12/05 Python
Python根据成绩分析系统浅析
2019/02/11 Python
python求平均数、方差、中位数的例子
2019/08/22 Python
pycharm设置当前工作目录的操作(working directory)
2020/02/14 Python
对Keras中predict()方法和predict_classes()方法的区别说明
2020/06/09 Python
python 如何上传包到pypi
2020/12/24 Python
巧用CSS3的calc()宽度计算做响应模式布局的方法
2018/03/22 HTML / CSS
Camper鞋西班牙官方网上商店:西班牙马略卡岛的鞋类品牌
2019/03/14 全球购物
长辈证婚人证婚词
2014/01/09 职场文书
九年级科学教学反思
2014/01/29 职场文书
打架检讨书500字
2014/01/29 职场文书
大学生在校学习的自我评价
2014/02/18 职场文书
电子工程专业毕业生求职信
2014/03/14 职场文书
采购求职信
2014/03/17 职场文书
体育教师求职信
2014/05/24 职场文书
五五普法心得体会
2014/09/04 职场文书
2016年3月份红领巾广播稿
2015/12/21 职场文书
2016年优秀共青团员事迹材料
2016/02/25 职场文书
写给消防战士们的一封慰问信
2019/10/07 职场文书