python实现按首字母分类查找功能


Posted in Python onOctober 31, 2019

本文实例为大家分享了python实现按首字母分类查找的具体代码,供大家参考,具体内容如下

要求:

1.自己查找一些英文词汇,存储到某个容器类中
2.根据英文词汇的首字母进行分类,类似于手机通讯簿中的快速查找功能
3.根据用户输入的字母,找到该字母开头的所有单词

#coding=utf-8
lexicons=["the","be","of","and","A","to","in","he","have","it","that","for","they","I","with","as","not","on","she","at","by","this","we","you","do","but","from","or","which","one","would","all","will","there","say","who","make","when","can"]
while True:
 startLetter=raw_input("输入一个字母,列出所有以此字母开头的单词:")
 if len(startLetter)!=1:
 print "必须是一个字母"
 else:
 reLexicons=[] #结果列表
 for x in xrange(len(lexicons)):
  lexicon=lexicons[x]
  if lexicon[0].lower()==startLetter.lower():#都转为小写后比较 开头字母不区分大小写
  reLexicons.append(lexicon)
 if len(reLexicons)==0:
  print "没有结果"
 else:
  for x in xrange(len(reLexicons)):
  print reLexicons[x]

上面的代码没有走第二步,如下代码 使用字典解决第二步

#coding=utf-8
'''
边遍历,边构造 key value 
'''
lexicons=["the","be","of","and","A","to","in","he","have","it","that","for","they","I","with","as","not","on","she","at","by","this","we","you","do","but","from","or","which","one","would","all","will","there","say","who","make","when","can"]
lexiconDict={}
#分类 保存字典中
lexiconLen=len(lexicons)
for x in xrange(len(lexicons)):
 lexicon=lexicons[x]
 startLetter=lexicon[0]
 dictLexicons=lexiconDict.get(startLetter,[])
  #空列表说明没有Key 则添加Key 否则追加Key对应的Value
 if len(dictLexicons)==0:
 lexiconDict[startLetter]=[lexicons[x]]
 else:
 dictLexicons.append(lexicons[x])
while True:
 startLetter=raw_input("输入一个字母,列出所有以此字母开头的单词:")
 if len(startLetter)!=1:
 print "必须是一个字母"
 else:
 lexicons=lexiconDict.get(startLetter.lower(),[])
 if len(lexicons)==0:
  print "没有结果"
 else:
  for x in lexicons:
  print x

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用go和python递归删除.ds store文件的方法
Jan 22 Python
Python统计文件中去重后uuid个数的方法
Jul 30 Python
Python文件处理
Feb 29 Python
python 3.0 模拟用户登录功能并实现三次错误锁定
Nov 01 Python
EM算法的python实现的方法步骤
Jan 02 Python
numpy.random.shuffle打乱顺序函数的实现
Sep 10 Python
解决python 上传图片限制格式问题
Oct 30 Python
python递归调用中的坑:打印有值, 返回却None
Mar 16 Python
Django Admin后台添加数据库视图过程解析
Apr 01 Python
在echarts中图例legend和坐标系grid实现左右布局实例
May 16 Python
python opencv实现简易画图板
Aug 27 Python
python 基于pygame实现俄罗斯方块
Mar 02 Python
利用python Selenium实现自动登陆京东签到领金币功能
Oct 31 #Python
python+Django实现防止SQL注入的办法
Oct 31 #Python
Python 爬虫实现增加播客访问量的方法实现
Oct 31 #Python
基于Python+Appium实现京东双十一自动领金币功能
Oct 31 #Python
如何关掉pycharm中的python console(图解)
Oct 31 #Python
使用python实现男神女神颜值打分系统(推荐)
Oct 31 #Python
python实现根据文件格式分类
Oct 31 #Python
You might like
用PHP连接Oracle数据库
2006/10/09 PHP
PHP 多进程 解决难题
2009/06/22 PHP
PHP中使用unset销毁变量并内存释放问题
2012/07/05 PHP
php不允许用户提交空表单(php空值判断)
2013/11/12 PHP
php实现telnet功能示例
2014/04/08 PHP
thinkphp5 模型实例化获得数据对象的教程
2019/10/18 PHP
学习js在线html(富文本,所见即所得)编辑器
2012/12/18 Javascript
JQuery中如何传递参数如click(),change()等具体实现
2013/04/28 Javascript
jQuery之ajax技术的详细介绍
2013/06/19 Javascript
GRID拖拽行的实例代码
2013/07/18 Javascript
jquery右下角自动弹出可关闭的广告层
2015/05/08 Javascript
纯JS前端实现分页代码
2016/06/21 Javascript
JS中图片压缩的方法小结
2017/11/14 Javascript
vue通过style或者class改变样式的实例代码
2018/10/30 Javascript
微信小程序实现分享朋友圈的图片功能示例
2019/01/18 Javascript
详解vue移动端项目代码拆分记录
2019/03/15 Javascript
Vue中keep-alive组件作用详解
2020/02/04 Javascript
Vue+Element-U实现分页显示效果
2020/11/15 Javascript
python读取浮点数和读取文本文件示例
2014/05/06 Python
python实现自动更换ip的方法
2015/05/05 Python
在Python中分别打印列表中的每一个元素方法
2018/11/07 Python
详解python中TCP协议中的粘包问题
2019/03/22 Python
python日志模块logbook使用方法
2019/09/19 Python
python图片剪裁代码(图片按四个点坐标剪裁)
2020/03/10 Python
Python常用数字处理基本操作汇总
2020/09/10 Python
化石印度尼西亚在线商店:Fossil Indonesia
2019/03/11 全球购物
Solid & Striped官网:美国泳装品牌
2019/06/19 全球购物
信息部岗位职责
2013/11/12 职场文书
工程测量与监理专业应届生求职信
2013/11/27 职场文书
新学期标语
2014/06/30 职场文书
2015年班长个人工作总结
2015/04/03 职场文书
地球上的星星观后感
2015/06/02 职场文书
休假证明书
2015/06/24 职场文书
投诉书格式范本
2015/07/02 职场文书
Mysql数据库命令大全
2021/05/26 MySQL
HTML+CSS 实现顶部导航栏菜单制作
2021/06/03 HTML / CSS