简体   繁体   中英

how to check some static string in python list?

I m checking full string that is exists or not in list

Here is the code give below for checking the strings in list python

str_text = ['SERVER_UPLOAD_FILE_PATH____::ad4d7360-9c6c-44fa-bcbb-0db7e671e036.png____SERVER_UPLOAD_END',
            'url was this SERVER_UPLOAD_FILE_PATH____::5e2650c2-728c-40af-99a4 
            eb100c432091.png____SERVER_UPLOAD_END and click here to see details']

if 'SERVER_UPLOAD_FILE_PATH____::5e2650c2-728c-40af-99a4-eb100c432091.png____SERVER_UPLOAD_END' in str_text:
       print('Exists')

i want to check both substrings SERVER_UPLOAD_FILE_PATH____:: & ____SERVER_UPLOAD_END that exits in the list elements and also single list elements also contain both

Any Help would be Appreciated & thanks in Advance

You would have to do the contains check on every string in the list:

pat = 'SERVER_UPLOAD_FILE_PATH____::5e2650c2-728c-40af-99a4-eb100c432091.png____SERVER_UPLOAD_END' in str_text

for s in str_text:
    if pat in s:
        print("Exists")
        break

There is a short-hand for that, any :

if any(pat in s for s in str_text):
    print("Exists")

You are only checking if the given pattern exists in str_text . What you really want to do is check if the pattern exists in any of the strings contained in str_text list.

You would have to check in every string contained in the str_text . You should modify your code as below -

str_text = ['SERVER_UPLOAD_FILE_PATH____::ad4d7360-9c6c-44fa-bcbb-0db7e671e036.png____SERVER_UPLOAD_END',
            'url was this SERVER_UPLOAD_FILE_PATH____::5e2650c2-728c-40af-99a4 eb100c432091.png____SERVER_UPLOAD_END and click here to see details']

for strings in str_text:
    if 'SERVER_UPLOAD_FILE_PATH____::ad4d7360-9c6c-44fa-bcbb-0db7e671e036.png____SERVER_UPLOAD_END' in strings:
       print('Exists')

A shorthand notation for the above code could be as follows -

str_text = ['SERVER_UPLOAD_FILE_PATH____::ad4d7360-9c6c-44fa-bcbb-0db7e671e036.png____SERVER_UPLOAD_END',
            'url was this SERVER_UPLOAD_FILE_PATH____::5e2650c2-728c-40af-99a4 eb100c432091.png____SERVER_UPLOAD_END and click here to see details']
pattern = 'SERVER_UPLOAD_FILE_PATH____::ad4d7360-9c6c-44fa-bcbb-0db7e671e036.png____SERVER_UPLOAD_END'
print(*("Exists" for strings in str_text if pattern in strings ))

The above code would print Exists for each string in str_text that matches your required pattern. If you just want it printed once, you could just apply break after the first match is found.

Hope this helps !

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