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 相关文章推荐
Python实现多行注释的另类方法
Aug 22 Python
用Python遍历C盘dll文件的方法
May 06 Python
python 字典 按key值大小 倒序取值的实例
Jul 06 Python
Python中的函数式编程:不可变的数据结构
Oct 08 Python
python判断计算机是否有网络连接的实例
Dec 15 Python
python 获取url中的参数列表实例
Dec 18 Python
Python socket 套接字实现通信详解
Aug 27 Python
Laravel框架表单验证格式化输出的方法
Sep 25 Python
Python实现钉钉订阅消息功能
Jan 14 Python
python判断字符串以什么结尾的实例方法
Sep 18 Python
python如何写个俄罗斯方块
Nov 06 Python
Python实现Matplotlib,Seaborn动态数据图
May 06 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
海河写的 Discuz论坛帖子调用js的php代码
2007/08/23 PHP
PHP持久连接mysql_pconnect()函数使用介绍
2012/02/05 PHP
sae使用smarty模板的方法
2013/12/17 PHP
PHP简单实现上一页下一页功能示例
2016/09/14 PHP
PHP封装返回Ajax字符串和JSON数组的方法
2017/02/17 PHP
PHP addAttribute()函数讲解
2019/02/03 PHP
js面向对象设计用{}好还是function(){}好(构造函数)
2011/10/23 Javascript
javascript仿qq界面的折叠菜单实现代码
2012/12/12 Javascript
js日期、星座的级联显示代码
2014/01/23 Javascript
JS获取文本框,下拉框,单选框的值的简单实例
2014/02/26 Javascript
node.js Web应用框架Express入门指南
2014/05/28 Javascript
网页下载文件期间如何防止用户对网页进行其他操作
2014/06/27 Javascript
Jquery实现$.fn.extend和$.extend函数
2016/04/14 Javascript
微信小程序 教程之WXSS
2016/10/18 Javascript
Radio 单选JS动态添加的选项onchange事件无效的解决方法
2016/12/12 Javascript
微信小程序之电影影评小程序制作代码
2017/08/03 Javascript
浅谈angular4生命周期钩子
2017/09/05 Javascript
使用JS获取页面上的所有标签
2018/10/18 Javascript
深入理解vue-class-component源码阅读
2019/02/18 Javascript
解决vue-cli@3.xx安装不成功的问题及搭建ts-vue项目
2020/02/09 Javascript
Json实现传值到后台代码实例
2020/06/30 Javascript
OpenLayer学习之自定义测量控件
2020/09/28 Javascript
JavaScript常用进制转换及位运算实例解析
2020/10/14 Javascript
全面解读Python Web开发框架Django
2014/06/30 Python
python编写的最短路径算法
2015/03/25 Python
简单说明Python中的装饰器的用法
2015/04/24 Python
Python中的引用知识点总结
2019/05/20 Python
如何将 awk 脚本移植到 Python
2019/12/09 Python
使用Html5、CSS实现文字阴影效果
2018/01/17 HTML / CSS
泰国排名第一的家居用品中心:HomePro
2020/11/18 全球购物
致接力运动员广播稿
2014/02/17 职场文书
小学英语复习计划
2015/01/19 职场文书
2015庆祝七一建党节94周年活动总结
2015/03/20 职场文书
python用tkinter开发的扫雷游戏
2021/06/01 Python
python批量创建变量并赋值操作
2021/06/03 Python
Python实现日志实时监测的示例详解
2022/04/06 Python