senten中的加长单词检查

2024-09-29 17:13:36 发布

您现在位置:Python中文网/ 问答频道 /正文

如果有长单词,我想检查句子。例如,soooo,toooo,thaaattt等等。现在我不知道用户可能会键入什么,因为我有一个句子列表,其中可能有也可能没有长单词。如何在python中检查它呢。我是python新手。在


Tags: 用户列表键入单词句子新手sooootoooo
3条回答

试试这个:

import re
s1 = "This has no long words"
s2 = "This has oooone long word"

def has_long(sentence):
    elong = re.compile("([a-zA-Z])\\1{2,}")
    return bool(elong.search(sentence))


print has_long(s1)
False
print has_long(s2)
True

好吧,你可以把每一个被拉长的单词列成一个逻辑上可行的列表。然后循环检查句子中的单词,然后在列表中找出延长的单词。在

sentence = "Hoow arre you doing?"
elongated = ["hoow",'arre','youu','yoou','meee'] #You will need to have a much larger list
for word in sentence:
    word = word.lower()
    for e_word in elongated:
        if e_word == word:
            print "Found an elongated word!"

如果你想照休·博思韦尔说的做,那么:

^{pr2}$

@HughBothwell有个好主意。据我所知,没有一个英语单词的同一个字母连续重复三次。因此,您可以搜索执行此操作的单词:

>>> from re import search
>>> mystr = "word word soooo word tooo thaaatttt word"
>>> [x for x in mystr.split() if search(r'(?i)[a-z]\1\1+', x)]
['soooo,', 'tooo', 'thaaatttt']
>>>

任何你找到的都会被拉长的词。在

相关问题 更多 >

    热门问题