简体   繁体   English

如何检查 python 列表中的一些 static 字符串?

[英]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这是下面给出的代码,用于检查列表 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我想检查在list元素中退出的两个子字符串SERVER_UPLOAD_FILE_PATH____:: & ____SERVER_UPLOAD_END并且单个列表元素也包含两者

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 :有一个简写形式, 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 .您只检查给定模式是否存在于str_text中。 What you really want to do is check if the pattern exists in any of the strings contained in str_text list.您真正想要做的是检查模式是否存在于str_text列表中包含的任何字符串中。

You would have to check in every string contained in the str_text .您必须检查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.上面的代码将为str_text中与您所需模式匹配的每个字符串打印Exists If you just want it printed once, you could just apply break after the first match is found.如果您只想打印一次,您可以在找到第一个匹配项后应用break

Hope this helps !希望这可以帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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