你的代码没有意义。
首先:使用x =...,您无法更改列表brands 上的值。你需要brands[index] = ...
第二:它需要嵌套for-loop 来比较x 和brands 中的所有其他词
for index, word in enumerate(brands):
for other in brands[index+1:]:
#print(word, other, fuzz.token_sort_ratio(word, other))
if fuzz.token_sort_ratio(word, other) > 85:
brands[index] = other
最少的工作代码
import pandas as pd
import fuzzywuzzy.fuzz as fuzz
data = {'brands':
'''Cool
Awesome
cool
CoOl
Awesum
Awesome
Mathss
Math
Maths
Mathss'''.split('\n')
} # rows
df = pd.DataFrame(data)
print('--- before ---')
print(df)
brands = df['brands'].to_list()
print('--- changes ---')
for index, word in enumerate(brands):
#for other_index, other_word in enumerate(brands):
for other_index, other_word in enumerate(brands[index+1:], index+1):
#if word != other_word:
result = fuzz.token_sort_ratio(word, other_word)
if result > 85:
print(f'OK | {result:3} | {index:2} {word:7} -> {other_index:2} {other_word}')
elif result > 50:
print(f' | {result:3} | {index:2} {word:7} -> {other_index:2} {other_word}')
if result > 85:
brands[index] = other_word
#break
#word = other_word
df['brands'] = brands
print('--- after ---')
print(df)
结果:
--- before ---
brands
0 Cool
1 Awesome
2 cool
3 CoOl
4 Awesum
5 Awesome
6 Mathss
7 Math
8 Maths
9 Mathss
--- changes ---
OK | 100 | 0 Cool -> 2 cool
OK | 100 | 0 Cool -> 3 CoOl
| 77 | 1 Awesome -> 4 Awesum
OK | 100 | 1 Awesome -> 5 Awesome
OK | 100 | 2 cool -> 3 CoOl
| 77 | 4 Awesum -> 5 Awesome
| 80 | 6 Mathss -> 7 Math
OK | 91 | 6 Mathss -> 8 Maths
OK | 100 | 6 Mathss -> 9 Mathss
OK | 89 | 7 Math -> 8 Maths
| 80 | 7 Math -> 9 Mathss
OK | 91 | 8 Maths -> 9 Mathss
--- after ---
brands
0 CoOl
1 Awesome
2 CoOl
3 CoOl
4 Awesum
5 Awesome
6 Mathss
7 Maths
8 Mathss
9 Mathss
它不会将Awesum 更改为Awesome,因为它会得到77
它不会将Math 更改为Mathss,因为它得到了80。但它得到89 为Maths。
如果你在for-loop 中使用word = other_word,那么它可以将Math 转换为Maths (89),然后将Maths 转换为Mathss (91)。但是这种方式可能会改变很多次,最后变成原来可以给出的值比85小得多的单词。 75 而不是 85 也可以获得预期结果。
但是这种方法得到的最后一个单词的值是>85,而不是最大的值——所以可以有更好的匹配单词,它不会使用它。使用 break 它得到>85 的第一个单词。也许它应该使用>85 获取所有单词并选择具有最大价值的单词。它必须跳过相同但在不同行中的单词。但这一切都会造成奇怪的情况。
在代码中的 cmets 中,我保留了其他修改的想法。
编辑:
>75 和颜色相同。
import pandas as pd
import fuzzywuzzy.fuzz as fuzz
from colorama import Fore as FG, Back as BG, Style as ST
data = {'brands':
'''Cool
Awesome
cool
CoOl
Awesum
Awesome
Mathss
Math
Maths
Mathss'''.split('\n')
} # rows
df = pd.DataFrame(data)
print('--- before ---')
print(df)
brands = df['brands'].to_list()
print('--- changes ---')
for index, word in enumerate(brands):
print('-', index, '-')
#for other_index, other_word in enumerate(brands):
for other_index, other_word in enumerate(brands[index+1:], index+1):
#if word != other_word:
result = fuzz.token_sort_ratio(word, other_word)
if result > 85:
color = ST.BRIGHT + FG.GREEN
info = 'OK'
elif result > 75:
color = ST.BRIGHT + FG.YELLOW
info = ' ?'
elif result > 50:
color = ST.BRIGHT + FG.WHITE
info = ' '
else:
color = ST.BRIGHT + FG.RED
info = ' -'
print(f'{color}{info} | {result:3} | {index:2} {word:7} -> {other_index:2} {other_word}{ST.RESET_ALL}')
if result > 75:
brands[index] = other_word
#break
#word = other_word
df['brands'] = brands
print('--- after ---')
print(df)