给定 Python 中包含项目的列表,找到项目索引的最快方法是什么?

Whats the fastest way of finding the indexes of an item given a list containing it in Python?

我有一个这样的列表,但包含数千甚至数百万个元素:

test = [
  ("goodbye", "help", "buy","sell", "1.1.1.1", 25), 
  ("hi dude", "text", "friends","love", "blablabla", 14), 
  ("hi darling", "books", "letter","class", "club", 64)
]

查找包含单词 "hi" 的所有元素索引的最优化代码是什么? 在上面给出的示例中,它应该 return test[1]test[2].

我认为这是最佳的;

word = "hi"
matches = []
for tupl in test:
    for elem in tupl:
        if word in elem:
            matches.append(tupl)
            break

您可以使用enumerateany来获取包含单词'hi'

的所有元素的索引
>>> test = [("goodbye", "help", "buy","sell", "1.1.1.1", 25), ("hi dude", "text", "friends","love", "blablabla", 14), ("hi darling", "books", "letter","class", "club", 64)]
>>> [i for i,words in enumerate(test) if any('hi' in str(word) for word in words)]
[1, 2]