用户输入的输入必须返回列表中所有带有其名称的索引

时间:2018-06-25 20:14:13

标签: python list

例如,如果我输入:"the"

我需要能够检测列表中包含"the"的所有标题。

my_list = ["The offspring", "legends", "the legends", "soraka"]

鉴于上面的列表,它应该返回"The offspring""the legends"。 如果输入全名,我就能检测到标题,但是当我被要求输入标题的一部分时,例如,我遇到了麻烦。 "the",目前还不确定要使用哪种函数或循环。

2 个答案:

答案 0 :(得分:0)

lst = ['the offspring','legends','the legends','soraka']

for i in lst:
    if 'the' in i:
        print(i)

这就是我要检查列表内部的方式,希望对您有所帮助!

答案 1 :(得分:0)

这种单线将为您处理。

filter(lambda x: "the" in x.lower().split(), ["The foo", "bar The", "baz"])

分裂的原因是只匹配整个单词。