python查找字符最快,python – 在字符串列表中搜索的高效且最快的方法

以下函数返回列表中包含与输入单词完全相同的字符的单词数.单词中字符的顺序并不重要.但是,有一个包含数百万字的列表.执行此搜索的最有效和最快的方法是什么?

例:

words_list = ['yek','lion','eky','ekky','kkey','opt'];

如果我们将单词“key”与列表中的单词匹配,则该函数仅返回“yek”和“eky”,因为它们与“key”共享相同的完全字符,而不管顺序如何.

以下是我写的功能

def find_a4(words_list, word):

# all possible permutations of the word that we are looking for

# it's a set of words

word_permutations = set([''.join(p) for p in permutations(word)])

word_size = len(word)

count = 0

for word in word_list:

# in the case of word "key",

# we only accept words that have 3 characters

# and they are in the word_permutations

if len(word) == word_size and word in word_permutations:

count += 1

return count

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值