0

I have a list and I want to find if the string is present in the list of strings.

li = ['Convenience','Telecom Pharmacy']
txt = '1 convenience store'

I want to match the txt with the Convenience from the list.

I have tried

if any(txt.lower() in s.lower() for s in li):
   print s

print [s for s in li if txt in s]

Both the methods didn't give the output.

How to match the substring with the list?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275

4 Answers4

3

You could use set() and intersection:

In [19]: set.intersection(set(txt.lower().split()), set(s.lower() for s in list1))
Out[19]: {'convenience'}
mvelay
  • 1,520
  • 1
  • 10
  • 23
0

You can try this,

>> list1 = ['Convenience','Telecom Pharmacy']

>> txt = '1 convenience store'

>> filter(lambda x: txt.lower().find(x.lower()) >= 0, list1)
['Convenience']

# Or you can use this as well

>> filter(lambda x: x.lower() in txt.lower(), list1)
['Convenience']
Anurag
  • 1,013
  • 11
  • 30
0

I see two things.

Do you want to find if the pattern string matches EXACTLY an item in the list? In this case, nothing simpler:

if txt in list1:
    #do something

You can also do txt.upper() or .lower() if you want list case insensitive But If you want as I understand, to find if there is a string (in the list) which is part of txt, you have to use "for" loop:

def find(list1, txt):
#return item if found, false otherwise
for i in list1:
    if i.upper() in txt.upper(): return i

return False

It should work.

Console output:

>>>print(find(['Convenience','Telecom Pharmacy'], '1 convenience store'))
Convenience
>>>
Vincent
  • 1,016
  • 6
  • 11
0

I think split is your answer. Here is the description from the python documentation:

string.split(s[, sep[, maxsplit]])

Return a list of the words of the string s. If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argument sep is present and not None, it specifies a string to be used as the word separator. The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string. If maxsplit is given, at most maxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).

The behavior of split on an empty string depends on the value of sep. If sep is not specified, or specified as None, the result will be an empty list. If sep is specified as any string, the result will be a list containing one element which is an empty string.

Use the split command on your txt variable. It will give you a list back. You can then do a compare on the two lists to find any matches. I personally would write the nested for loops to check the lists manually, but python provides lots of tools for the job. The following link discusses different approaches to matching two lists.

How can I compare two lists in python and return matches

Enjoy. :-)

Community
  • 1
  • 1
codingCat
  • 2,396
  • 4
  • 21
  • 27