10

For example, where:

list = [admin, add, swear]
st = 'siteadmin'

st contains string admin from list.

  • How can I perform this check?
  • How can I be informed which string from list was found, and if possible where (from start to finish in order to highlight the offending string)?

This would be useful for a blacklist.

5
  • possible duplicate of How to check if a string contains an element from a list in Python Commented Aug 20, 2015 at 15:00
  • I'd also like to know which string it was, and if possible where it is, but that answer is still definitely helpful, thanks Commented Aug 20, 2015 at 15:06
  • 1
    You will probably need regex since you need to know complete word matches so that a partial match isn't treated as a false positive. For example ignoring 'ass' in 'assertion' == True Commented Aug 20, 2015 at 15:08
  • can that be done in regex? it's almost like I need a whitelist to check against when a blacklisted string is found... Commented Aug 20, 2015 at 15:09
  • 1
    don't name variables as 'list', it conflicts with built-in python. Commented Jul 31, 2019 at 13:48

5 Answers 5

19
list = ['admin', 'add', 'swear']
st = 'siteadmin'
if any([x in st for x in list]):print "found"
else: print "not found"

You can use any built-in function to check if any string in the list appeared in the target string

Sign up to request clarification or add additional context in comments.

Comments

3

You can do this by using list-comprehessions

ls = [item for item in lst if item in st]

UPD: You wanted also to know position :

ls = [(item,st.find(item)) for item in lst if st.find(item)!=-1]

Result : [('admin', 4)

You can find more information about List Comprehensions on this page

2 Comments

How does ls = [item for item in lst if item in st] work?
@StringsOnFire this is called List Comprehensions. It means do some operation with every item in list if some condition holds. In this case condition is st.find(item)!=-1.
1

I am assuming the list is very large. So in this program, I am keeping the matched items in a list.

#declaring a list for storing the matched items
matched_items = []
#This loop will iterate over the list
for item in list:
    #This will check for the substring match
    if item in st:
        matched_items.append(item)
#You can use this list for the further logic
#I am just printing here 
print "===Matched items==="
for item in matched_items:
    print item

Comments

1

Is this what you are looking for?

for item in list:
    if item in st:
        print(item)
        break
    else:
        print("No string in list was matched")

Comments

0
for x in list:
     loc = st.find(x)
     if (loc != -1):
          print x
          print loc

string.find(i) returns the index of where the substr i begins in st, or -1 on failure. This is the most intuitive answer in my opinion, you can make this probably into a 1 liner, but I'm not a big fan of those usually.

This gives the extra value of knowing where the substring is found in the string.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.