为什么我的词典程序返回随机单词?

时间:2015-08-18 11:37:44

标签: python regex python-3.x

我制作了一个程序,允许用户输入一个字母,然后显示以该字母开头的单词。我从这个页面得到了我的程序的实际字母:

How to find a word that starts with a specific character

具体来说:re.findall(r' \ bs \ w +',text)

为了让用户能够输入任何字母,我创建了一个输入(字母)和一部分代码,将该输入转换为大写和小写变量,称为letterupper和letterlower。然后我改变了代码:

words = re.findall(r'\bs\w+', text)

为:

words = re.findall(r'\b[(lowerletter)(upperletter)]\w+', text)

问题是当我现在运行我的程序时,它会根据您输入的输入提供随机单词。例如,我输入了字母K,它给了我以r,u,p,t等开头的单词... lowerletter和upperletter变量确实存储了正确的信息,因为当我打印它们时它们包含大小写版本我输入的那封信。我不知道发生了什么。

完整代码减去以下几百字:

import re
text = "dialogue language rhythm a diamond lap abandon diary large rib ability dictate largely ribbon able die laser rice abortion diet last rich about differ late rid above difference lately ride abroad different later rider absence differently Latin ridge absolute difficult latter ridiculous absolutely difficulty laugh rifle absorb dig laughter right abstract digital launch rim abuse dignity law ring academic dilemma lawmaker riot accelerate dimension lawn rip accent diminish lawsuit rise accept dining lawyer risk acceptable dinner lay risky acceptance dip layer ritual access diplomat lead rival accessible diplomatic leader river accident direct leadership road accommodate direction leading robot accompany directly leaf rock accomplish director league rocket accomplishment dirt lean rod according dirty leap role account disability learn roll accountability disabled learning rolling accounting disagree least Roman accuracy disappear leather romance accurate disappointed leave romantic accurately disappointment lecture roof accusation disaster left room accuse disc leg root achieve discipline legacy rope achievement disclose legal rose acid discount legally rough acknowledge discourage legend roughly acquire discourse legislation round acquisition discover legislative route across discovery legislator routine act discrimination legislature routinely action discuss legitimate row active discussion lemon royal actively disease lend rub activist dish length rubber activity disk lens ruin actor dismiss less rule actress disorder lesson ruling actual display let rumor actually dispute letter run ad dissolve level runner adapt distance liability running add distant liberal rural added distinct liberty rush addition distinction library Russian additional distinctive license sack address distinguish lid sacred adequate distract lie sacrifice adjust distribute life sad adjustment distribution lifestyle safe administer district lifetime safely administration disturb lift safety administrative disturbing light sail administrator diverse lighting sake admire diversity lightly salad admission divide lightning salary admit divine like sale adolescent division likelihood sales adopt divorce likely salmon adoption DNA likewise salt adult do limb same advance dock limit sample advanced doctor limitation sanction advantage doctrine limited sand adventure document line sandwich advertising documentary link satellite advice dog lion satisfaction advise doll lip satisfy adviser domain liquid sauce advocate domestic list save aesthetic dominant listen saving affair dominate listener say affect donate literally scale afford donation literary scan afraid donor literature scandal African door little scare African-American doorway live scared after dose liver scary afternoon dot living scatter afterward double load scenario again doubt loan scene against dough lobby scent age down local schedule agency downtown locate scheme agenda dozen location scholar agent draft lock scholarship aggression drag log school aggressive drain logic science ago drama logical scientific agree dramatic lonely scientist agreement dramatically long scope agricultural draw long-term score agriculture drawer longtime scramble ah drawing look scratch ahead dream loop scream aid dress loose screen aide dried lose screening AIDS drift loss screw aim drill lost script air drink lot sculpture aircraft drinking lots sea airline drive loud seal airplane driver love  cotton indicate racial vast couch indication racism vegetable could indicator rack vehicle council indigenous radar vendor counsel individual radiation venture counseling industrial radical verbal counselor industry radio verdict count inevitable rage version counter inevitably rail versus counterpart infant railroad vertical country infection rain very county inflation raise vessel coup influence rally veteran couple influential ranch via courage inform random victim course informal range victory court information rank video courtroom infrastructure rape view cousin ingredient rapid viewer cover inherent rapidly village coverage inherit rare violate cow initial rarely violation crack initially rat violence craft initiate rate violent crash initiative rather virtual crawl injure rating virtually crazy injury ratio virtue cream inmate rational virus create inner raw visible creation innocent re vision creative innovation reach visit creativity innovative react visitor creature input reaction visual credibility inquiry read vital credit insect reader vitamin crew insert readily vocal crime inside reading voice criminal insight ready volume crisis insist real voluntary criteria inspection realistic volunteer critic inspector reality vote critical inspiration realize voter criticism inspire really voting criticize install realm vs crop installation rear vulnerable cross instance reason wage crowd instant reasonable wagon crowded instantly rebel waist crucial instead rebuild wait cruel instinct recall wake cruise institution receive walk crush institutional receiver walking cry instruct recent wall crystal instruction recently wander Cuban instructional reception want cue instructor recession war cultural instrument recipe warehouse culture insurance recipient warm cup intact recognition warmth cure integrate recognize warn curiosity integrated recommend warning curious integration recommendation warrior currency integrity record wash current intellectual recording waste currently intelligence recover watch curriculum intelligent recovery water curtain intend recruit wave curve intense red way custody intensity reduce we custom intent reduction weak customer intention refer weaken cut interact reference weakness cute interaction reflect wealth cycle interest reflection wealthy dad interested reform weapon daily interesting refrigerator wear dam interfere refuge weather damage interior refugee weave damn internal refuse web dance international regain wedding dancer Internet regard weed dancing interpret regarding week danger interpretation regardless weekend dangerous interrupt regime weekly dare interval region weigh dark intervention regional weight darkness interview register weird data intimate regret welcome database into regular welfare date introduce regularly well daughter introduction regulate well-being dawn invade regulation well-known day invasion regulator west dead invent regulatory western deadline invention rehabilitation wet deadly inventory reinforce whale deal invest reject what dealer investigate relate whatever dear investigation related wheat death investigator relation wheel debate investment relationship wheelchair debris investor relative when debt invisible relatively whenever debut invitation relax where decade invite release whereas decent involve relevant wherever decide involved reliability whether decision involvement reliable which deck Iraqi relief while declare Irish relieve whip decline iron religion whisper decorate ironically religious white decrease irony reluctant who dedicate Islam rely whoever deem Islamic remain whole deep island remaining whom deeply isolate remark whose deer isolated remarkable why defeat isolation remember wide defend Israeli remind widely defendant issue reminder widespread defender it remote widow defense Italian removal wife defensive item remove wild deficit its render wilderness define itself rent wildlife definitely jacket rental will definition jail repair willing degree Japanese repeat willingness delay jar repeatedly win deliberately jaw replace wind delicate jazz replacement window delight jeans reply wine deliver jet report wing delivery Jew reportedly winner demand jewelry reporter winter democracy Jewish reporting wipe Democrat job represent wire democratic join representation wisdom demographic joint representative wise demonstrate joke republic wish demonstration journal Republican with denial journalism reputation withdraw dense journalist request withdrawal density journey require within deny joy required without depart judge requirement witness department judgment rescue wolf departure judicial research woman depend juice researcher wonder dependent jump resemble wonderful depending jungle reservation wood depict junior reserve wooden deploy jurisdiction residence word deposit juror resident work depressed jury residential worker depression just resign working depth justice resist workout deputy justify resistance workplace derive keep resolution works descend key resolve workshop describe kick resort world description kid resource worldwide desert kill respect worried deserve killer respectively worry design killing respond worth designer kind respondent would desire king response wound desk kingdom responsibility wow desperate kiss responsible wrap desperately kit rest wrist despite kitchen restaurant write dessert knee restore writer destination kneel restrict writing destroy knife restriction written destruction knock result wrong detail know resume yard detailed knowledge retail yeah detect known retailer year detective Korean retain yell determination lab retire yellow determine label retired yes devastating labor retirement yesterday develop laboratory retreat yet developer lack return yield developing ladder reveal you development lady revelation young developmental lake revenue youngster device lamp reverse your devil land review yours devote landing revolution yourself diabetes landmark revolutionary youth diagnose landscape reward zone diagnosis lane rhetoric  ."
letter = input ("Please Enter the Letter you want to find the words beggining with.")
upperletter = letter.upper()
lowerletter = letter.lower()
words = re.findall(r'\b[(lowerletter)(upperletter)]\w+', text)
print (words)

2 个答案:

答案 0 :(得分:2)

使用+运算符将变量与原始正则表达式连接起来。

words = re.findall(r'\b[' + lowerletter + upperletter +r']\w+', text)

答案 1 :(得分:0)

对于像这样的简单检查,正则表达式可能有点太多了;任务可以使用标准字符串方法完成。

用split()分解文本,我们比较每个单词的第一个字母降低到输入,在着名列表理解中:

letter = raw_input ("Please Enter the Letter you want to find the words beggining with: ")
words = [word for word in text.split() if word.lower()[0] == letter.lower())
print (words)

一些有趣的观点:使用raw_input,因为它不被解释;如果您将word.lower()[0]替换为word.lower().startswith( letter.lower() ),则可以检查多个字符。最后,如果我们使用生成器,它的内存效率更高(而且更有趣:);最后两行是:

words = (word for word in text.split() if word.lower()[0] == letter.lower())
for word in words:
    print word