<p>您可以使用标准发行版中的<a href="http://docs.python.org/library/difflib.html" rel="nofollow noreferrer">difflib</a>执行此操作:</p>
<pre><code>import difflib
s="""R Deep Transverse Metatarsal Ligament 4 GEODE
R Distal JointCapsule 1 GEODE
R Dorsal Calcaneocuboid Ligament GEODE
R Dorsal Carpometacarpal Ligament 2 GEODE
R Dorsal Cuboideavicular Ligament GEODE
R Dorsal Tarsometatarsal Ligament 5 GEODE
R Elbow Capsule GEODE
R F Distal JointCapsule 1 GEODE
R Fibular Collateral Bursa GEODE
R Fibular Collateral Ligament GEODE
R Fibular Ligament GEODE""".split('\n')
qs="""R De Me Li
Fi Colla
bow ODE""".split('\n')
for q in qs:
print "results for '{}':".format(q)
matches=difflib.get_close_matches(q,s,3,0.3)
for i,e in enumerate(matches,1):
print "\t{}. {}".format(i,e)
</code></pre>
<p>印刷品:</p>
^{pr2}$
<p>结合<a href="https://stackoverflow.com/a/10924574/298607">cblab's answer</a>结合regex和difflib,可以得到:</p>
<pre><code>s="""R Deep Transverse Metatarsal Ligament 4 GEODE
R Distal JointCapsule 1 GEODE
R Dorsal Calcaneocuboid Ligament GEODE
R Dorsal Carpometacarpal Ligament 2 GEODE
R Dorsal Cuboideavicular Ligament GEODE
R Dorsal Tarsometatarsal Ligament 5 GEODE
R Elbow Capsule GEODE
R F Distal JointCapsule 1 GEODE
R Fibular Collateral Bursa GEODE
R Fibular Collateral Ligament GEODE
R Fibular Ligament GEODE""".split('\n')
s=set(s)
qs="""R De Me Li
Fi Colla
bow ODE
Car
Do Car
ral lar
R De Me 4
R Do Carp""".split('\n')
for q in sorted(qs):
print "results for '{}':".format(q)
pattern = r'.*' + re.sub(r'\W', '.*', q.strip()) + '.*'
matches=[item for item in s if re.match(pattern, item, re.I)]
for e in difflib.get_close_matches(q,s,3,0.33):
if e not in matches:
matches.append(e)
for i,e in enumerate(matches,1):
print "\t{}. {}".format(i,e)
else:
if len(matches)==0:
print "\tNo matches"
</code></pre>
<p>印刷品:</p>
<pre><code>results for 'Car':
1. R Dorsal Carpometacarpal Ligament 2 GEODE
results for 'Do Car':
1. R Dorsal Carpometacarpal Ligament 2 GEODE
results for 'Fi Colla':
1. R Fibular Collateral Bursa GEODE
2. R Fibular Collateral Ligament GEODE
results for 'R De Me 4':
1. R Deep Transverse Metatarsal Ligament 4 GEODE
results for 'R De Me Li':
1. R Deep Transverse Metatarsal Ligament 4 GEODE
2. R Dorsal Calcaneocuboid Ligament GEODE
results for 'R Do Carp':
1. R Dorsal Carpometacarpal Ligament 2 GEODE
2. R Elbow Capsule GEODE
3. R Distal JointCapsule 1 GEODE
results for 'bow ODE':
1. R Elbow Capsule GEODE
results for 'ral lar':
No matches
</code></pre>