<p>可以使用<a href="https://github.com/seatgeek/fuzzywuzzy" rel="nofollow noreferrer">fuzzy wuzzy</a>库替换字符串。制作一个团队的主列表,然后您可以使用fuzzy-wuzzy包来提取与其他字符串最相似的团队。但它并不完美,因为您需要测试不同的场景,例如<code>'CAR'</code>不会返回<code>Carolina Panthers</code>,它将返回<code>'Arizona Cardinals'</code>,并且<code>'LVR'</code>返回了<code>'Los Angels Rams'</code>。因此,你只需要支付,然后可能对于那些少数情况,只需要制定一些逻辑来找回正确的团队</p>
<pre><code>from fuzzywuzzy import process
nfl_teams = ['Arizona Cardinals', 'Atlanta Falcons', 'Carolina Panthers', 'Chicago Bears',
'Dallas Cowboys', 'Detriot Lions','Green Bay Packers','Los Angeles Rams',
'Minnesota Vikings','New Orleans Saints','New York Giants', 'Philadelphia Eagles',
'San Francisco 49ers','Seattle Seahawks','Washington Redskins','Baltimore Ravens',
'Buffalo Bills','Cinncinnati Bangals','Cleveland Browns','Denver Broncos',
'Houston Texans','Indanapolis Colts','Jacksonville Jaguars','Kansas City Chiefs',
'Las Vegas Raiders','Los Angeles Chargers','Miami Dolphins','New England Patriots',
'New York Jets','Pittsburgh Steelers','Tennessee Titans']
matchups = ['OAK Raiders vs CHI Bears','GB Packers vs MIN Vikings','PIT Steelers vs LA Rams', 'PHI vs DEN']
for each in matchups:
team1, team2 = each.split('vs')[0].strip(), each.split('vs')[-1].strip()
team1_alpha = process.extractOne(team1, nfl_teams)[0]
team2_alpha = process.extractOne(team2, nfl_teams)[0]
print ('%s -> %s vs %s' %(each, team1_alpha, team2_alpha))
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>OAK Raiders vs CHI Bears -> Las Vegas Raiders vs Chicago Bears
GB Packers vs MIN Vikings -> Green Bay Packers vs Minnesota Vikings
PIT Steelers vs LA Rams -> Pittsburgh Steelers vs Los Angeles Rams
PHI vs DEN -> Philadelphia Eagles vs Denver Broncos
</code></pre>
<p>或者,如果你想做一个比较,看看他们有多相似</p>
<pre><code>from fuzzywuzzy import fuzz
var1 = "Pittsburgh Steelers vs Los Angeles Rams"
var2 = "PIT Steelers vs LA Rams"
print(fuzz.ratio(var1, var2))
print(fuzz.token_sort_ratio(var1, var2))
print(fuzz.token_set_ratio(var1, var2))
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>68
71
82
</code></pre>