我怎样才能使我的功能 运行 更快?

How can i make my function run even more faster?

大家好,我正在解决问题 我对我的第一个函数做了很多更改以达到时间限制

但这真的是我最后的想法,我不知道如何让它比现在更快

from timeit import default_timer as timer
from datetime import timedelta

start = timer()

testList = ['hello']
wordList = ['hello','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe']
def words_with_given_shape(words,shape):
    itemList = 'abcdefghijklmnopqrstuvwxyz'
    def wordShape(word):
        tempS = []
        termil = len(word)-1
        
        for inx,elem in enumerate(word):
            orderAl = itemList.index(elem)
            if inx < termil:
                orderNl = itemList.find(word[inx+1])
                if orderAl > orderNl:
                    tempS.append(-1)
                if orderAl < orderNl:
                    tempS.append(1)
                if orderNl == orderAl:
                    tempS.append(0) 
        return tempS
    def checkWord(words):
        res = []
        for i in words:
            if wordShape(i)==shape:
                res.append(i)
        return res
    return checkWord(words)
print(words_with_given_shape(wordList,  [-1, 1, 0, 1,1,1,-1]))
print(words_with_given_shape(wordList,  [-1, 1, 0, 1]))
print(words_with_given_shape(wordList,  [-1, 1]))
print(words_with_given_shape(wordList,  [-1, 1, 0, 1,1, 0, 1,-1,1]))
print(words_with_given_shape(testList,  [-1, 1, 0, 1]))
end = timer()
print(timedelta(seconds=end-start))

它现在给我这次 0:00:00.001272

但测试人员似乎需要比这更快的速度,因为在测试 12 中由于执行时间限制而失败

所以基本上你能指导我使 words_with_given_shape 功能更加优化吗?

*** 编辑 ***: 我忘了告诉问题是它给出了单词列表和单词形状 形状像 [0,1,1,-1] 这意味着 0当量 1 个字符在字母顺序中位于当前字符之后 -1 个字符在字母顺序中位于当前字符之前

你好 它的 [-1, 1, 0, 1]

答案是找到单词列表中形状与 arg 形状相同的所有单词

通过像所示那样重新格式化您的代码并使用 List Comprehensions,您在 10000 次迭代中的速度提高了 100 毫秒:

  • 初始化:
from timeit import default_timer as timer
from datetime import timedelta
start = timer()
testList = ['hello']
wordList = ['hello','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe','helloetttttttttttttttttttttttttwersdfffffffffffffffffffffffffffffffavvvvvvvvvvvvvvvvvvvvvvvvvqwewqe']
  • v1:
def checkcond(x, i, word, itemList):
    _, __ = itemList.index(x),  itemList.find(word[i+1])
    return -1 if _ > __ else 1 if _ < __ else 0

def wordShape(word, itemList):
    return [checkcond(x, i, word, itemList) for i, x in enumerate(word[:-1])]

def words_with_given_shape(words,shape):
    itemList = 'abcdefghijklmnopqrstuvwxyz'
    return [x for x in words if wordShape(x, itemList)==shape]
  • v2:(在 google colab 上更快)
def words_with_given_shape(words,shape):
    itemList = 'abcdefghijklmnopqrstuvwxyz'
    def checkcond(x, i, word):
        _, __ = itemList.index(x),  itemList.find(word[i+1])
        return -1 if _ > __ else 1 if _ < __ else 0
    def wordShape(word):
        return [checkcond(x, i, word) for i, x in enumerate(word[:-1])]
    return [x for x in words if wordShape(x)==shape]
  • 时间检查:
def timecheck():
    for x in range(10000):
        words_with_given_shape(wordList,  [-1, 1, 0, 1,1,1,-1])
        words_with_given_shape(wordList,  [-1, 1, 0, 1])
        words_with_given_shape(wordList,  [-1, 1])
        words_with_given_shape(wordList,  [-1, 1, 0, 1,1, 0, 1,-1,1])
        words_with_given_shape(testList,  [-1, 1, 0, 1])
    return timedelta(seconds=timer()-start)
print(timecheck())

试试这个:

def words_with_given_shape(words, shape):
    return [word for word in words
            if (len(shape) == len(word) - 1 and
                all(c1 == c2 if s == 0 else (c1 > c2 if s == -1 else c1 < c2)
                    for c1, c2, s in zip(word[:-1], word[1:], shape)))]

它更紧凑(只有一行)并且您不需要生成每个单词的形状(在这种情况下这是无用的,因为您可以使用提供的形状)!