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?

2
  • 3
    A needle bigger than a haystack will never be found inside a haystack. Commented Mar 31, 2016 at 12:46
  • @AlexReynolds pretty ironic, isn't it ? Commented Mar 31, 2016 at 12:48

4 Answers 4

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'}
Sign up to request clarification or add additional context in comments.

1 Comment

I think something like set(txt.lower().split()) & set(s.lower() for s in list1) would be easier to read
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. :-)

1 Comment

The example from massiou makes use of split in his intersection command.
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
>>>

Comments

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']

2 Comments

Why not use lambda x: x.lower() in txt?
I think we can use this as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.