8

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.

StringsOnFire
  • 2,726
  • 5
  • 28
  • 50
  • possible duplicate of [How to check if a string contains an element from a list in Python](http://stackoverflow.com/questions/6531482/how-to-check-if-a-string-contains-an-element-from-a-list-in-python) – Mel Aug 20 '15 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 – StringsOnFire Aug 20 '15 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` – Josh J Aug 20 '15 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... – StringsOnFire Aug 20 '15 at 15:09
  • 1
    don't name variables as 'list', it conflicts with built-in python. – geher Jul 31 '19 at 13:48

5 Answers5

18
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

Hooting
  • 1,681
  • 11
  • 20
2

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

ig-melnyk
  • 2,769
  • 2
  • 25
  • 35
  • How does `ls = [item for item in lst if item in st]` work? – StringsOnFire Aug 21 '15 at 11:09
  • @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. – ig-melnyk Aug 21 '15 at 11:39
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")
Y4RD13
  • 937
  • 1
  • 16
  • 42
SPKB24
  • 37
  • 4
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
Amal G Jose
  • 2,486
  • 1
  • 20
  • 35
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.

Eugene K
  • 3,381
  • 2
  • 23
  • 36