1

I'm trying to get a piece of code to return the index of where the match was found within the list, so that another item can be compared to this index. (essentially allowing two separate strings to be compared to the same item on a list). I'm able to get the match if it matches exactly, but can't get it to work with a partial string. Here is some pseudo code to show what I have:

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)

So my question is how can I get the index of 'mno' to return so that I can compare str2 against it after the first check. If there's a better way around this problem id love to hear it

Connor J
  • 540
  • 1
  • 6
  • 17
  • Possible duplicate of [Finding a substring within a list in Python](https://stackoverflow.com/questions/13779526/finding-a-substring-within-a-list-in-python) – metatoaster Jun 19 '18 at 13:12
  • `str` is a whole string (by the way, don't name variables by python classes). Did you want to split it into words? – OneCricketeer Jun 19 '18 at 13:13
  • @metatoaster that link doesn't give the index of the element in the list – Connor J Jun 19 '18 at 13:14
  • 1
    So you're saying that "mno" should be considered a match because "mn" is in the strings? Does this also mean that "def" should be a match, because "e" is in the strings? – Kevin Jun 19 '18 at 13:14
  • @cricket_007 yeah ideally it would be split into words and checked for any matches if possible – Connor J Jun 19 '18 at 13:14
  • @Kevin mn should be considered a match because it is a whole word, whereas e wouldn't because it isn't a whole word - if that makes sense? – Connor J Jun 19 '18 at 13:16
  • My suggestion would be to rewrite with loops, then later you can do list comprehension – OneCricketeer Jun 19 '18 at 13:17
  • Ok, sounds good to me. Wasn't sure if word boundaries were involved. – Kevin Jun 19 '18 at 13:20
  • 1
    @ConnorJ This would be better match then [python - find index position in list based of partial string](https://stackoverflow.com/questions/14849293/python-find-index-position-in-list-based-of-partial-string/14849322) – metatoaster Jun 19 '18 at 13:23
  • @metatoast That's essentially the same code in the question, but `aa` in that question comes from elements of another string – OneCricketeer Jun 19 '18 at 13:26

5 Answers5

2

How about like this:

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

...or if you're looking for a list comprehension:

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

Output:

[4]
Ashish Acharya
  • 3,349
  • 1
  • 16
  • 25
2

From the question, str1 in s will never be true since no element of mylist contains the entire str1 string.

Assuming word boundaries are important, you can split the string, then find indexes of the string containing that token

b = [] 
for s1 in str1.split():
    for i, s2 in enumerate(mylist):
        if s1 in s2:
            b.append(i)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Maybe this one can help!!

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)
0

Maybe it would be best to put this into a function?

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)

Output:

[4]
[]
JimmyA
  • 676
  • 5
  • 13
0

This may help as in str2 you are searching specific keyword which is "mn" so we split str1 into simple list of words and then traverse the list to find occurrences of partial mn in items.

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)
  • 3
    While this code may answer the question, providing additional [context](https://meta.stackexchange.com/q/114762) regarding _how_ and/or _why_ it solves the problem would improve the answer's long-term value. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add an explanation, and give an indication of what limitations and assumptions apply. It also doesn't hurt to mention why this answer is more appropriate than others. – Dev-iL Jun 19 '18 at 14:34