简体   繁体   中英

How to check if a list contains a specific words which is part of another list

Hello I am new to python this may be something small but I am finding it difficult on how to get output for this

I have below list "sgList" which I want to check if it contains any of the specific words which is another list "checkforstrings" if yes then return true otherwise false

sgList   = ['Sensitive-ce-Public-TF_', 'cb-access', 'convc-service']

checkforstrings = ["-Public-", "-Private-", "-Protected-"]

in the above case this should return true coz '-Public-' is present in 'Sensitive-ce-Public-TF_'

I know how to find a specific word in list but this may either in the starting/ending or middle so not sure how to check this Any help would be greatly appreciated thank you

One option is to wrap a generator expression in any . The idea is we iterate over each string in checkforstrings and see if it's in any string in sgList :

out = any(s in sg for s in checkforstrings for sg in sgList)

Output:

True

You can use the operator in .

If '-Public-' in 'Sensitive-ce-Public-TF '

Would return true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM