如何在列表变量中查找位置,由字符串[Python]搜索

如何在列表变量中查找位置,由字符串[Python]搜索,python,list,Python,List,我有一张这样的清单 result = ['Alice in the forest - 01.mp4', 'Code-009 - 02.mp4', 'Art 7 - 01.mp4', 'Will be owned - 05.mp4'] 像这样的变量 search = 'Alice' 有没有办法在列表中搜索并通过关键字'Alice'查找'Alice in the forest-01.mp4',并将变量的编号保存在列表中 附言:直到现在,我一直在尝试string=re.compile(“Alice

我有一张这样的清单

result = ['Alice in the forest - 01.mp4', 'Code-009 - 02.mp4', 'Art 7 - 01.mp4', 'Will be owned - 05.mp4']
像这样的变量

search = 'Alice'
有没有办法在列表中搜索并通过关键字
'Alice'
查找
'Alice in the forest-01.mp4'
,并将变量的编号保存在列表中

附言:直到现在,我一直在尝试
string=re.compile(“Alice”)
。但没有打印出任何东西


提前感谢。

要获取包含搜索词的所有字符串,您可以使用:

match_string = [s for s in result if "Alice" in s]
match_string = [i for i, s in enumerate(result) if "Alice" in s]
要获取包含搜索词的所有字符串的位置,可以使用:

match_string = [s for s in result if "Alice" in s]
match_string = [i for i, s in enumerate(result) if "Alice" in s]

要获取包含搜索词的所有字符串,可以使用:

match_string = [s for s in result if "Alice" in s]
match_string = [i for i, s in enumerate(result) if "Alice" in s]
要获取包含搜索词的所有字符串的位置,可以使用:

match_string = [s for s in result if "Alice" in s]
match_string = [i for i, s in enumerate(result) if "Alice" in s]

您需要遍历该列表,并查找您的搜索词是否出现在当前项中

def searchWordInList(input_list, word):
    index_and_value = []
    for i in range(len(input_list)):
        if word in input_list[i]:
            index_and_value .append(i, input_list[i])
    return index_and_value 
result = ['Alice in the forest - 01.mp4', 'Code-009 - 02.mp4', 'Art 7 - 01.mp4', 'Will be owned - 05.mp4']
search = 'Alice'

index_and_value = searchWordInList(result, search)

您需要遍历该列表,并查找您的搜索词是否出现在当前项中

def searchWordInList(input_list, word):
    index_and_value = []
    for i in range(len(input_list)):
        if word in input_list[i]:
            index_and_value .append(i, input_list[i])
    return index_and_value 
result = ['Alice in the forest - 01.mp4', 'Code-009 - 02.mp4', 'Art 7 - 01.mp4', 'Will be owned - 05.mp4']
search = 'Alice'

index_and_value = searchWordInList(result, search)

您也可以使用and函数执行此操作:

result = ['Alice in the forest - 01.mp4', 'Code-009 - 02.mp4', 'Art 7 - 01.mp4', 'Will be owned - 05.mp4']
search = 'Alice'
output = list(filter(lambda song: search in song[1], enumerate(result)))
print(output)
输出:

[(0, 'Alice in the forest - 01.mp4')]

您也可以使用and函数执行此操作:

result = ['Alice in the forest - 01.mp4', 'Code-009 - 02.mp4', 'Art 7 - 01.mp4', 'Will be owned - 05.mp4']
search = 'Alice'
output = list(filter(lambda song: search in song[1], enumerate(result)))
print(output)
输出:

[(0, 'Alice in the forest - 01.mp4')]
enumerate()返回所有索引中出现的“Alice”


enumerate()返回所有索引中出现的“Alice”

您所说的“保存列表中变量的编号”是什么意思?@Ev.Kounis想找到列表中的位置ex)Alice in the forest=0,Code-009=1“保存列表中变量的编号”是什么意思?@Ev.Kounis想找到列表中的位置,例如)林中的Alice=0,代码-009=1