擅长:python、mysql、java
<p>避免包含子字符串的一种方法是使用正则表达式,如:</p>
<pre><code>import re
pdfList = ["01 Violin I.pdf", "02 Violin I.pdf","01 Violin II.pdf", "02 Violin \
II.pdf", ]
instruments = ["Soprano", "Tenor", "Violin I", "Violin II", "Viola", "Cello", "\
Contrabass", "Alto Saxophone I", "Alto Saxophone II", "Tenor Saxophone", "Barit\
one Saxophone"]
# create arrays for each instrument that can be used for merging/organization
def organizer():
for fileName in pdfList:
tempList = []
for instrument in instruments:
if re.search(r'\b{}\b'.format(instrument), fileName):
tempList.append(fileName)
print tempList
print pdfList
organizer()
</code></pre>
<p>这将用<code>\b</code>包装您的搜索词,以便仅当开头和结尾在单词边界上时匹配。另外,也许很明显,但值得指出的是,这也会使你的工具名成为regex的一部分,所以要注意,如果你使用的任何字符也是regex元字符,它们将被解释为这样(现在你不是)。一个更通用的方案需要一些代码来找到并正确地转义这些字符。你知道吗</p>