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之Socket网络编程详解
Sep 29 Python
Python中如何获取类属性的列表
Dec 26 Python
python实现机械分词之逆向最大匹配算法代码示例
Dec 13 Python
python判断一个对象是否可迭代的例子
Jul 22 Python
numpy ndarray 按条件筛选数组,关联筛选的例子
Nov 26 Python
Python udp网络程序实现发送、接收数据功能示例
Dec 09 Python
Pytorch to(device)用法
Jan 08 Python
PyCharm License Activation激活码失效问题的解决方法(图文详解)
Mar 12 Python
Python3 shelve对象持久存储原理详解
Mar 23 Python
python用tkinter开发的扫雷游戏
Jun 01 Python
Python中的 Set 与 dict
Mar 13 Python
Python内置的数据类型及使用方法
Apr 13 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
PHP5 的对象赋值机制介绍
2011/08/02 PHP
php中使用Ajax时出现Error(c00ce56e)的详细解决方案
2014/11/03 PHP
php访问数组最后一个元素的函数end()用法
2015/03/18 PHP
javascript flash下fromCharCode和charCodeAt方法使用说明
2008/01/12 Javascript
jQuery AJAX实现调用页面后台方法和web服务定义的方法分享
2012/03/01 Javascript
jquery 为a标签绑定click事件示例代码
2014/06/23 Javascript
JavaScript控制网页层收起和展开效果的方法
2015/04/15 Javascript
JS简单限制textarea内输入字符数量的方法
2015/10/14 Javascript
详解JavaScript正则表达式之RegExp对象
2015/12/13 Javascript
AngularJS用户选择器指令实例分析
2016/11/04 Javascript
jQuery 常见小例汇总
2016/12/14 Javascript
Vue.js系列之vue-router(上)(3)
2017/01/03 Javascript
详解Angualr 组件间通信
2017/01/21 Javascript
实现div内部滚动条滚动到底部和顶部的代码
2017/11/15 Javascript
vue v-for 使用问题整理小结
2019/08/04 Javascript
JavaScript本地储存:localStorage、sessionStorage、cookie的使用
2020/10/13 Javascript
[08:40]Navi Vs Newbee
2018/06/07 DOTA
python编写网页爬虫脚本并实现APScheduler调度
2014/07/28 Python
python循环监控远程端口的方法
2015/03/14 Python
使用基于Python的Tornado框架的HTTP客户端的教程
2015/04/24 Python
python排序方法实例分析
2015/04/30 Python
详解Python if-elif-else知识点
2018/06/11 Python
python将一组数分成每3个一组的实例
2018/11/14 Python
python3实现名片管理系统
2020/11/29 Python
基于wxPython的GUI实现输入对话框(1)
2019/02/27 Python
Python按钮的响应事件详解
2019/03/04 Python
python对绑定事件的鼠标、按键的判断实例
2019/07/17 Python
Python实现一个数组除以一个数的例子
2019/07/20 Python
使用虚拟环境打包python为exe 文件的方法
2019/08/29 Python
html5时钟实现代码
2010/10/22 HTML / CSS
FORZIERI福喜利中国官网:奢侈品购物梦工厂
2019/05/03 全球购物
大学生校园创业计划书
2014/02/08 职场文书
法人代表证明书范本
2015/06/18 职场文书
Python上下文管理器Content Manager
2021/06/26 Python
Spring Boot 排除某个类加载注入IOC的操作
2021/08/02 Java/Android
JAVA 线程池(池化技术)的实现原理
2022/04/28 Java/Android