首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从字符串列表中,如何获得包含3个或3个以上字符且在python中间隔均匀的元组/单词列表?

从字符串列表中,如何获得包含3个或3个以上字符且在python中间隔均匀的元组/单词列表?
EN

Stack Overflow用户
提问于 2012-04-14 05:05:59
回答 2查看 115关注 0票数 0

可能重复: 检查字符串的字符是否按字母顺序上升,其上升是否为均匀间隔的python

我有一个字符串/单词的列表:

代码语言:javascript
复制
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循环,但在这种情况下我不知道如何使用它,也不知道如何执行第二部分。

关于上面的清单,我应该得到的答案是:

代码语言:javascript
复制
([])

我没有最新版本的蟒蛇。

任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

发布于 2012-04-14 05:16:42

您可能应该从学习如何使用用于循环开始。for循环将从集合中获取内容并将它们赋值给一个变量:

代码语言:javascript
复制
for letter in "strings are collections":
    print letter

或者..。

代码语言:javascript
复制
for thing in ['this is a list', 'of', 4, 'things']:
    if thing == 4:
        print '4 is in the list'

如果你能做的比这更多,那就尝试一些事情,找出你被困在哪里,更具体地问你需要什么帮助。

票数 0
EN

Stack Overflow用户

发布于 2012-04-14 06:00:34

分步骤解决这个问题

  1. 筛选长度为>= 3的单词 [w for w in mylist if len(w) >= 3]
  2. 看单词是否在有规律的间隔中增加?计算连续字母之间的差异,创建一个集合并检查长度是否为== 1 diff =lambda word:len({ord(n)-ord(c) for n,c in zip(word[1:],word)}) == 1
  3. 现在使用这个新函数过滤剩余的单词。 [w for w in mylist if len(w) >= 3 and diff(w)]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10151183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档