在Python的单词列表中查找三个单词

2024-09-24 00:35:03 发布

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

形容词big的比较形式较大,最高级形式最大。我想打印出所有这样的三倍(早,早,早)或(硬,硬,硬),。。。你知道吗

我在用Python打开wordList.txt文件大约有5000字。我在一个小文件上完美地测试了我的代码,但是它不能在大文件上运行,因为循环太长了。你知道吗

def puzzleA(wordList):
    tempList1 = []
    tempList2 = []
    tempList3 = []
    for word in wordList:
        if word[-2:]=='er':
            tempList1.append(word)
        if word[-3:]=='est':
            tempList2.append(word)            

    for word1 in wordList:
        for word2 in tempList1:
            if word1==word2[:-2]:
                tempList3.append(word1)
    for word1 in tempList3:
        for word2 in tempList2:
            if word1==word2[:-3]:
                print('{}, {}er, {}'.format(word1,word1,word2))      

你们能推荐另一种算法来优化运行时间吗!你知道吗


Tags: 文件inforif形式worderwordlist
3条回答

你现在似乎在努力学习如何用一致的方式来词干。 对于您的场景,您可以使用模式替换规则列表,即“如果单词以this模式结尾,则将其替换为that”。 使用正则表达式,您可以很容易地指定模式和替换,包括“如果它以一个重复的字母结尾,则用该字母的一个实例替换它”之类的内容。你知道吗

例如:

def root(word):
  pattern_replacements = [
    ("e$", ""),    # fine => fin (to match finer, finest)
    ("y$", "i"),   # tiny => tini (to match tinier, tiniest)
    ("er$", ""),
    ("est$", ""),
    (r"([a-z])\1$", r"\1")  # bigger => big 
  ]

  for pattern, replacement in pattern_replacements:
    word = re.sub(pattern, replacement, word)

  return word


words = "big bigger biggest tiny tinier tiniest fine finer finest same samer samest good gooder goodest".split(" ")
map(root, words)
# ['big', 'big', 'big', 'tini', 'tini', 'tini', 'fin', 'fin', 'fin', 'sam', 'sam', 'sam', 'good', 'good', 'good']

非常感谢你发邮件给蒂埃里·拉图伊尔。我已经四个小时没看你的答案了。我不太懂你的密码。我确实像这样调整了root(word)函数:

def root(word):
    if len(word) < 4:
        return word
    if word[-3:] == 'ier':
        return word[:-3] + 'y'
    elif word[-4:] == 'iest':
        return word[:-4] + 'y'
    elif word[-2:] == 'er':
        if word[-4:-3]==word[-3:-2]:
            return word[:-3]
        else:
            return word[:-2]
    elif word[-3:] == 'est':
        if word[-4:-3]==word[-5:-4]:
            return word[:-4]        
        return word[:-3]
    else:
        return word

但它现在有两个问题: 首先,单词列表中有重复的单词,所以它会产生类似[terry,terry,terrier]的内容。你知道吗

第二,很难找到这么大的三倍

我的产品[呜呜,呜呜,呜呜],[威利,威利,威利],[斯莱特,斯莱特,斯莱特],。。。你知道吗

假设不允许我先删除重复的单词。那么有没有办法访问每个键中的每个值呢。我想比较一下这些对值,以消除不需要的结果。你知道吗

蒂埃里,如果你有时间,能解释一下密码吗?你知道吗

out = [sorted(values, key=len) for values in out_dict.values() if len(values)==3]

我真的不擅长阅读清单。你知道吗

您可以构建一个dict,将单词的根作为键,将列表中的所有变体作为值。那么我们只保留3个值的条目。这样,我们只在列表上迭代一次,在我们创建的dict上迭代一次,使整个过程保持O(n)。你知道吗

我们可以使用defaultdict更容易地构建dict。请注意root函数可能需要一些改进,请查看英语形容词列表!你知道吗

from collections import defaultdict


def root(word):
    if len(word) < 4:
        return word
    if word[-3:] == 'ier':
        return word[:-3] + 'y'
    elif word[-4:] == 'iest':
        return word[:-4] + 'y'
    elif word[-2:] == 'er':
        return word[:-2]
    elif word[-3:] == 'est':
        return word[:-3]
    else:
        return word

def find_triples(words):
    out_dict = defaultdict(list)
    for word in words:
        out_dict[root(word)].append(word)

    # keep only the lists with 3 distinct values, sorted by length
    out = [sorted(set(values), key=len) for values in out_dict.values() 
                                   if len(set(values))==3]
    return out


data = ['early', 'earlier', 'earliest', 'or', 'hard', 'harder', 'hardest', 'ignored']
print(find_triples(data))
# [['early', 'earlier', 'earliest'], ['hard', 'harder', 'hardest']]

相关问题 更多 >