在 Python 中的 if 语句中对列表使用迭代器
Using iterator over a list within the if statement in Python
我正在使用一个函数来读取特定文件,在本例中是 options
并为我读取的每一行做一些正则表达式。我正在阅读的文件是:
EXE_INC = \
-I$(LIB_SRC)/me/bMesh/lnInclude \
-I$(LIB_SRC)/mTools/lnInclude \
-I$(LIB_SRC)/dynamicM/lnInclude
我的密码是
def libinclude():
with open('options', 'r') as options:
result = []
for lines in options:
if 'LIB_SRC' in lines and not 'mTools' in lines:
lib_src_path = re.search(r'\s*-I$\(LIB_SRC\)(?P<lpath>\/.*)', lines.strip())
lib_path = lib_src_path.group(1).split()
result.append(lib_path[0])
print result
return (result)
现在如您所见,我查找具有 mTools
的行并使用 not 'mTools' in lines
进行过滤。但是,当我有很多这样的字符串时,我该如何过滤呢?例如,我想过滤具有 mTools
和 dynamicM
的行。是否可以将此类字符串放入列表中,然后在 if
语句中根据 lines
访问该列表的元素?
我正在使用一个函数来读取特定文件,在本例中是 options
并为我读取的每一行做一些正则表达式。我正在阅读的文件是:
EXE_INC = \
-I$(LIB_SRC)/me/bMesh/lnInclude \
-I$(LIB_SRC)/mTools/lnInclude \
-I$(LIB_SRC)/dynamicM/lnInclude
我的密码是
def libinclude():
with open('options', 'r') as options:
result = []
for lines in options:
if 'LIB_SRC' in lines and not 'mTools' in lines:
lib_src_path = re.search(r'\s*-I$\(LIB_SRC\)(?P<lpath>\/.*)', lines.strip())
lib_path = lib_src_path.group(1).split()
result.append(lib_path[0])
print result
return (result)
现在如您所见,我查找具有 mTools
的行并使用 not 'mTools' in lines
进行过滤。但是,当我有很多这样的字符串时,我该如何过滤呢?例如,我想过滤具有 mTools
和 dynamicM
的行。是否可以将此类字符串放入列表中,然后在 if
语句中根据 lines
访问该列表的元素?