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
。