本文實例為大家分享了python實現(xiàn)按首字母分類查找的具體代碼,供大家參考,具體內(nèi)容如下
成都創(chuàng)新互聯(lián)公司長期為成百上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為吐魯番企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、做網(wǎng)站,吐魯番網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。要求:
1.自己查找一些英文詞匯,存儲到某個容器類中
2.根據(jù)英文詞匯的首字母進行分類,類似于手機通訊簿中的快速查找功能
3.根據(jù)用戶輸入的字母,找到該字母開頭的所有單詞
#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=[] #結(jié)果列表 for x in xrange(len(lexicons)): lexicon=lexicons[x] if lexicon[0].lower()==startLetter.lower():#都轉(zhuǎn)為小寫后比較 開頭字母不區(qū)分大小寫 reLexicons.append(lexicon) if len(reLexicons)==0: print "沒有結(jié)果" else: for x in xrange(len(reLexicons)): print reLexicons[x]