python实现按首字母分类查找功能
作者:强强强子 时间:2023-10-13 11:05:09
本文实例为大家分享了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
来源:https://blog.csdn.net/sinat_25926481/article/details/71703102
标签:python,字母分类,查找
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使table也能overflow:hidden
2008-08-18 21:04:00
[翻译]标记语言和样式手册 chapter 5 表单
2008-01-23 17:20:00
![](https://img.aspxhome.com/file/UploadPic/20081/23/2008123183735445s.jpg)
MySQL 四种连接和多表查询详解
2024-01-14 17:34:29
![](https://img.aspxhome.com/file/2023/7/80137_0s.png)
给zblog加上运行代码功能
2007-12-19 13:07:00
Anaconda出现CondaHTTPError: HTTP 000 CONNECTION FAILED for url的解决过程
2021-05-12 11:30:27
![](https://img.aspxhome.com/file/2023/8/82718_0s.png)
删除PHP数组中头部、尾部、任意元素的实现代码
2023-06-14 12:40:44
![](https://img.aspxhome.com/file/2023/1/55471_0s.png)
使用一条INSERT语句完成多表插入
2010-03-18 11:08:00
Python calendar模块详情
2023-08-20 23:04:59
![](https://img.aspxhome.com/file/2023/9/67799_0s.png)
在Python web中实现验证码图片代码分享
2022-05-29 05:32:50
![](https://img.aspxhome.com/file/2023/6/95746_0s.gif)
CSS自适应宽度圆角按钮
2007-11-20 11:38:00
![](https://img.aspxhome.com/file/UploadPic/200711/20/20071120115213701s.gif)
Golang import本地包和导入问题相关详解
2024-01-30 09:35:37
![](https://img.aspxhome.com/file/2023/2/103722_0s.png)
Go语言中的UTF-8实现
2024-04-25 15:30:51
JS实现长图上下滚动效果
2023-07-22 10:28:26
![](https://img.aspxhome.com/file/2023/7/55967_0s.gif)
从零学python系列之教你如何根据图片生成字符画
2022-04-03 16:05:50
![](https://img.aspxhome.com/file/2023/1/130651_0s.png)
详解python列表(list)的使用技巧及高级操作
2021-05-12 15:26:47
分享216色网页拾色器(调色板)
2007-09-27 12:33:00
![](https://img.aspxhome.com/file/UploadPic/20079/27/2007927124954557s.jpg)
python socket多线程通讯实例分析(聊天室)
2022-11-21 13:18:58
网站制作流程及界面交互设计
2007-10-26 12:00:00
![](https://img.aspxhome.com/file/UploadPic/200710/26/2007102612842449s.jpg)
关于PowerDesigner初体验的使用介绍
2024-01-13 04:15:25
![](https://img.aspxhome.com/file/2023/2/119222_0s.jpg)
PyQt5中QTimer定时器的实例代码
2021-06-01 07:28:54
![](https://img.aspxhome.com/file/2023/7/93797_0s.png)