Python-检查字符串的一部分是否存在于列表中,并给出其匹配项的索引

Python-检查字符串的一部分是否存在于列表中,并给出其匹配项的索引,python,Python,我正在尝试获取一段代码来返回列表中匹配项所在位置的索引,以便将另一项与此索引进行比较。(基本上允许将两个单独的字符串与列表中的同一项进行比较)。如果匹配项完全匹配,我可以获取匹配项,但无法使用部分字符串。下面是一些伪代码,以显示我所拥有的: mylist = ['abc', 'def', 'ghi', 'jkl', 'mno'] str1 = 'does mn have anything to do with it?' str2 = 'I think mn is there' b = [i f

我正在尝试获取一段代码来返回列表中匹配项所在位置的索引,以便将另一项与此索引进行比较。(基本上允许将两个单独的字符串与列表中的同一项进行比较)。如果匹配项完全匹配,我可以获取匹配项,但无法使用部分字符串。下面是一些伪代码,以显示我所拥有的:

mylist = ['abc', 'def', 'ghi', 'jkl', 'mno']
str1 = 'does mn have anything to do with it?'
str2 = 'I think mn is there'

b = [i for i, s in enumerate(mylist) if str1 in s]
print(b)
所以我的问题是,我如何才能让'mno'的索引返回,以便在第一次检查后将str2与它进行比较。如果有更好的方法来解决这个问题,我很乐意听你这么说

b = []
for i in str1.split():
    for idx, j in enumerate(mylist):
        if i in j:
            b.append(idx)
…或者如果您正在查找列表:

b = [idx for i in str1.split() for idx, j in enumerate(mylist) if i in j]
输出:

[4]
[4]
[]
这样怎么样:

b = []
for i in str1.split():
    for idx, j in enumerate(mylist):
        if i in j:
            b.append(idx)
…或者如果您正在查找列表:

b = [idx for i in str1.split() for idx, j in enumerate(mylist) if i in j]
输出:

[4]
[4]
[]

从问题中可以看出,
str1在s
中永远不会为真,因为
mylist
的任何元素都不包含整个
str1
字符串

假设单词边界很重要,您可以拆分字符串,然后查找包含该标记的字符串的索引

b = [] 
for s1 in str1.split():
    for i, s2 in enumerate(mylist):
        if s1 in s2:
            b.append(i)

从问题中可以看出,
str1在s
中永远不会为真,因为
mylist
的任何元素都不包含整个
str1
字符串

假设单词边界很重要,您可以拆分字符串,然后查找包含该标记的字符串的索引

b = [] 
for s1 in str1.split():
    for i, s2 in enumerate(mylist):
        if s1 in s2:
            b.append(i)

也许这个能帮上忙

mylist = ['abc', 'def', 'ghi', 'jkl', 'mno']
str1 = 'does mn have anything to do with it?'
str2 = 'I think mn is there'

b = [i  for i, s in enumerate(mylist) for st in str2.split(' ') if st in s]
print(b)

也许这个能帮上忙

mylist = ['abc', 'def', 'ghi', 'jkl', 'mno']
str1 = 'does mn have anything to do with it?'
str2 = 'I think mn is there'

b = [i  for i, s in enumerate(mylist) for st in str2.split(' ') if st in s]
print(b)

也许最好将其放入函数中

str = 'does mno have anything to do with it?'
str2 = 'I think mn is there'
mylist = ['abc', 'def', 'ghi', 'jkl', 'mno']

def are_you_here(str, list):
    return [list.index(x) for x in list if x in str]

are_you_here(str, mylist)
are_you_here(str2, mylist)
输出:

[4]
[4]
[]

也许最好将其放入函数中

str = 'does mno have anything to do with it?'
str2 = 'I think mn is there'
mylist = ['abc', 'def', 'ghi', 'jkl', 'mno']

def are_you_here(str, list):
    return [list.index(x) for x in list if x in str]

are_you_here(str, mylist)
are_you_here(str2, mylist)
输出:

[4]
[4]
[]

这可能会有所帮助,因为在str2中,您正在搜索特定的关键字,即“mn”,因此我们将str1拆分为简单的单词列表,然后遍历该列表以查找项目中出现的部分mn

mylist = ['abc', 'def', 'ghi', 'jkl', 'mno']

str1 = 'does mn have anything to do with it?'
str2 = 'I think mn is there'

for i in str1.split(" "):
    for j in mylist:
    if i in j:
        print("match found and its for " + i + " which is partially exist in " + j)

这可能会有所帮助,因为在str2中,您正在搜索特定的关键字,即“mn”,因此我们将str1拆分为简单的单词列表,然后遍历该列表以查找项目中出现的部分mn

mylist = ['abc', 'def', 'ghi', 'jkl', 'mno']

str1 = 'does mn have anything to do with it?'
str2 = 'I think mn is there'

for i in str1.split(" "):
    for j in mylist:
    if i in j:
        print("match found and its for " + i + " which is partially exist in " + j)

str
的可能副本是一个完整的字符串(顺便说一下,不要用python类命名变量)。你想把它拆分成单词吗?@metatoaster该链接没有给出列表中元素的索引,所以你是说“mno”应该被视为匹配,因为“mn”在字符串中?这是否也意味着“def”应该是一个匹配项,因为“e”在字符串中?@ConnorJ这将比可能重复的
str
是一个完整的字符串更好(顺便说一句,不要用python类命名变量)。你想把它拆分成单词吗?@metatoaster该链接没有给出列表中元素的索引,所以你是说“mno”应该被视为匹配,因为“mn”在字符串中?这是否也意味着“def”应该是匹配的,因为“e”在字符串中?@ConnorJ如果问题没有使用
str2
,那么这会更好地匹配,所以我不确定这是被问的问题。问题没有使用
str2
,所以我不确定这是被问的问题。问题想要索引,哪个是enumerate被使用的问题需要索引,哪个是enumerate被使用的问题谢谢!这是完美的作品,是好的和短-理想的我正在做的!您可以使用嵌套列表理解,如其他答案所示,但我发现这些很难阅读谢谢!这是完美的作品,是好的和短-理想的我正在做的!您可以使用嵌套列表理解,如其他答案中所示,但我发现这些理解很难阅读,而这段代码可能会回答这个问题,提供关于如何和/或为什么解决问题的额外信息将提高答案的长期价值。记住,你是在将来回答读者的问题,而不仅仅是现在提问的人!请在回答中添加解释,并说明适用的限制和假设。提到为什么这个答案比其他答案更合适也没什么坏处。虽然这段代码可以回答这个问题,但提供关于如何和/或为什么它解决问题的额外信息将提高答案的长期价值。记住,你是在将来回答读者的问题,而不仅仅是现在提问的人!请在回答中添加解释,并说明适用的限制和假设。提到为什么这个答案比其他答案更合适也没什么坏处。