我有一个字符串/单词的列表:
mylist = ['twas', 'brillig', 'and', 'the', 'slithy', 'toves', 'did', 'gyre', 'and', 'gimble', 'in', 'the', 'wabe', 'all', 'mimsy', 'were', 'the', 'borogoves', 'and', 'the', 'mome', 'raths', 'outgrabe', '"beware', 'the', 'jabberwock', 'my', 'son', 'the', 'jaws', 'that', 'bite', 'the', 'claws', 'that', 'catch', 'beware', 'the', 'jubjub', 'bird', 'and', 'shun', 'the', 'frumious', 'bandersnatch', 'he', 'took', 'his', 'vorpal', 'sword', 'in', 'hand', 'long', 'time', 'the', 'manxome', 'foe', 'he', 'sought', 'so', 'rested', 'he', 'by', 'the', 'tumtum', 'tree', 'and', 'stood', 'awhile', 'in', 'thought', 'and', 'as', 'in', 'uffish', 'thought', 'he', 'stood', 'the', 'jabberwock', 'with', 'eyes', 'of', 'flame', 'came', 'whiffling', 'through', 'the', 'tulgey', 'wood', 'and', 'burbled', 'as', 'it', 'came', 'one', 'two', 'one', 'two', 'and', 'through', 'and', 'through', 'the', 'vorpal', 'blade', 'went', 'snicker-snack', 'he', 'left', 'it', 'dead', 'and', 'with', 'its', 'head', 'he', 'went', 'galumphing', 'back', '"and', 'has', 'thou', 'slain', 'the', 'jabberwock', 'come', 'to', 'my', 'arms', 'my', 'beamish', 'boy', 'o', 'frabjous', 'day', 'callooh', 'callay', 'he', 'chortled', 'in', 'his', 'joy', '`twas', 'brillig', 'and', 'the', 'slithy', 'toves', 'did', 'gyre', 'and', 'gimble', 'in', 'the', 'wabe', 'all', 'mimsy', 'were', 'the', 'borogoves', 'and', 'the', 'mome', 'raths', 'outgrabe']首先,我只需要得到其中包含3个或3个以上字符的单词--我假设它是一个for循环或者什么的。然后,我需要得到一个单词列表,其中只包含从左到右字母递增的单词,并且是一个固定的数字相隔。(例如('ace',2)或('ceg',2)不必是2)列表也必须按字母顺序排序,每个元素应该是由单词和字符差异组成的元组。
我认为我必须使用for循环,但在这种情况下我不知道如何使用它,也不知道如何执行第二部分。
关于上面的清单,我应该得到的答案是:
([])我没有最新版本的蟒蛇。
任何帮助都是非常感谢的。
发布于 2012-04-14 05:16:42
您可能应该从学习如何使用用于循环开始。for循环将从集合中获取内容并将它们赋值给一个变量:
for letter in "strings are collections":
print letter或者..。
for thing in ['this is a list', 'of', 4, 'things']:
if thing == 4:
print '4 is in the list'如果你能做的比这更多,那就尝试一些事情,找出你被困在哪里,更具体地问你需要什么帮助。
发布于 2012-04-14 06:00:34
分步骤解决这个问题
[w for w in mylist if len(w) >= 3]diff =lambda word:len({ord(n)-ord(c) for n,c in zip(word[1:],word)}) == 1[w for w in mylist if len(w) >= 3 and diff(w)]https://stackoverflow.com/questions/10151183
复制相似问题