startswith
和in
,返回布尔值- 该
in
运营商成员的考验。 - 可以使用
list-comprehension
或来执行filter
list-comprehension
与一起使用in
是经过测试的最快实施。- 如果大小写不是问题,请考虑将所有单词映射为小写。
l = list(map(str.lower, l))
。
filter:
使用
filter
会创建一个filter
对象,因此list()
用于显示中的所有匹配值list
。l = [‘ones’, ‘twos’, ‘threes’]
using startswith
wanted = ‘three’result = list(filter(lambda x: x.startswith(wanted), l))
using inresult = list(filter(lambda x: wanted in x, l))
print(result)
[out]:
[‘threes’]
list-comprehension
哪个实现更快?l = ['ones', 'twos', 'threes']wanted = 'three'# using startswithresult = [v for v in l if v.startswith(wanted)]# using inresult = [v for v in l if wanted in v]print(result)[out]:['threes']
- 使用
words
语料库nltk
'three'
['three', 'threefold', 'threefolded', 'threefoldedness', 'threefoldly', 'threefoldness', 'threeling', 'threeness', 'threepence', 'threepenny', 'threepennyworth', 'threescore', 'threesome']
from nltk.corpus import words
%timeit list(filter(lambda x: x.startswith(wanted), words.words()))
[out]:
47.4 ms ± 1.9 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)%timeit list(filter(lambda x: wanted in x, words.words()))
[out]:
27 ms ± 1.78 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)%timeit [v for v in words.words() if v.startswith(wanted)]
[out]:
34.1 ms ± 768 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)%timeit [v for v in words.words() if wanted in v]
[out]:
14.5 ms ± 63.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)