Writeamethodanagrams(f)thatopensatextfile,f,splitsitintowordsandprintsoutalistofanagramsinthefile.Recallthattwowordsareanagramsiftheyarethesameafterwesorttheirlettersalph...
Write a method anagrams(f) that opens a text file, f, splits it into words and prints out a list of anagrams in the file. Recall that two words are anagrams if they are the same after we sort their letters alphabetically. To sort the letters in a word, w, you can use this code: key = "".join(sorted([c for c in w])).
测试是这样子的。。words.txt是一个全是英文单词的txt
...
>>> anagrams('words.txt')
'emrsu': remus serum
'ceiklnrs': crinkles clinkers
'ainrst': trinas trains strain
'abbcsy': cabbys scabby
'ailprs': aprils spiral
'chins': inchs chins
'degilr': girdle glider
'giorsv': virgos vigors
'belru': bluer ruble
'aknor': akron koran
'ainst': tinas antis nitas saint stain satin
'egiinprrs': springier reprising respiring
'airstt': traits artist
'eerst': steer reste trees
'eersv': serve sever
'aceht': cheat teach
'adeprs': spared spread
'adeprt': parted depart
'eilms': smile miles
'lvx': xlv lxv
'below': elbow below
大概思路(我也不知道对不对)把每个txt导成2维数组。例如car:【0】【0】是c【0】【1】是a【0】【2】是r。然后循环回去配对。。。就是不太会写。。。。急求谢谢
展开