python - 用于查找不同格式字符串之间相似性的 Python 函数
问题描述
我有 2 个带有项目名称的 excel 文件。我想比较这些项目,但唯一远程相似的列是名称列,它也具有不同的名称格式,例如
KIDS-钢琴作为儿童钢琴
黄油凝胶 100mg作为Butter-Gel-100MG
我知道它不可能 100% 准确,所以我会要求操作代码的人进行最终验证,但我如何显示最接近的匹配名称?
解决方案
这样做的正确方法是编写正则表达式。
但是下面的香草代码也可以解决问题:
column_a = ["KIDS-Piano", "Butter Gel 100mg"]
column_b = ["kids piano", "Butter-Gel-100MG"]
new_column_a = []
for i in column_a:
# convert strings into lowercase
a = i.lower()
# replace dashes with spaces
a = a.replace('-', ' ')
new_column_a.append(a)
# do the same for column b
new_column_b = []
for i in column_b:
# convert strings into lowercase
a = i.lower()
# replace dashes with spaces
a = a.replace('-', ' ')
new_column_b.append(a)
as_not_found_in_b = []
for i in new_column_a:
if i not in new_column_b:
as_not_found_in_b.append(i)
bs_not_found_in_a = []
for i in new_column_b:
if i not in new_column_a:
bs_not_found_in_a.append(i)
# find the problematic ones and manually fix them
print(as_not_found_in_b)
print(bs_not_found_in_a)
推荐阅读
- azure-language-understanding - 当在使用 Node.js 的 KB QnA maker 中找不到匹配项时,如何配置 LUIS 响应?
- r - 渲染存储在 R 闪亮应用程序列表中的多个图
- python - 查找 url 的正则表达式直到某个深度/目录
- kotlin - 如何在运行时获取函数文字的注解
- python - Python 游戏逻辑 - 类似口袋妖怪的类型差异
- javascript - jQuery Magnify:为“data-magnify-src”添加相同的图像
- android - Android:Paging3:重复项目
- c# - 滚动期间面板背景不刷新(C#/VB.net)
- python - Pyside2中的2D可点击表面
- excel - Excel - VBA 网页抓取 - getElementsByTagName