Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中if语句包含重复字符的字符串_Python_String_If Statement_Duplicates - Fatal编程技术网

Python中if语句包含重复字符的字符串

Python中if语句包含重复字符的字符串,python,string,if-statement,duplicates,Python,String,If Statement,Duplicates,我试图使用if语句来检查是否存在一个特定的字符串,该字符串恰好也具有相同的文件名。但是,我遇到了两个具有公共字符的字符串,我希望严格不要检查重复的字符串。这是我代码的一部分。我有两个文件名为p_rgh_0和h_0 for path, dirs, files in os.walk(logsDir, topdown=False): for lf in sorted(files): if (plot_pressure or plot_all): if any( (filenam

我试图使用
if
语句来检查是否存在一个特定的字符串,该字符串恰好也具有相同的文件名。但是,我遇到了两个具有公共字符的字符串,我希望严格不要检查重复的字符串。这是我代码的一部分。我有两个文件名为
p_rgh_0
h_0

for path, dirs, files in os.walk(logsDir, topdown=False):
for lf in sorted(files):
    if (plot_pressure or plot_all):
        if any( (filename in lf) for filename in ('p_0', 'p_rgh_0') ):
            logFile = os.path.join(path, lf)
            data = np.loadtxt(logFile)
            time, value = data[:,0], data[:,1]
            (time, value, lf) = pressure(time, value, lf)
    if (plot_enthalpy):
        if any( (filename in lf) for filename in ('h_0') ):
            print lf
            logFile = os.path.join(path, lf)
            data = np.loadtxt(logFile)
            time, value = data[:,0], data[:,1]
            (time, value, lf) = enthalpy(time, value, lf)

现在,由于
h_0
是一个公共字符串,因此这两个文件都在第二个
if
语句中处理,其中我只检查
h_0
print lf
打印两个文件,但它应该仅为
h_0
。有没有办法消除这种行为,只检查
h_0

您可以在第一个块处理
pressure()
中使用
continue
语句,以确保任何双匹配文件不会在
焓()中处理
调用。

您是否可以使用
==
而不是
中的
进行测试,或者确实存在需要文件名的子字符串才能工作的情况?我实际上不需要文件名的子字符串才能工作。例如
p_rgh_0
h_0
是完全不同的。我可以使用
=
,例如,
如果lf=='hf'
,但是我将来可能会增加字符串列表,这样我就有更多的字符串从
h
开始,包括
h\u 0