python - 两个字符串(句子)之间的区别
问题描述
我试图计算两个句子之间的差异,如下所示:
import difflib
text1_lines = "I understand how customers do their choice. Difference"
text2_lines = "I understand how customers do their choice."
diff = difflib.ndiff(text1_lines, text2_lines)
我想有所作为
但我不明白。我究竟做错了什么 ?谢谢你让我知道。
解决方案
来自文档:
import difflib
import sys
text1_lines = "I understand how customers do their choice. Difference"
text2_lines = "I understand how customers do their choice."
diff = difflib.context_diff(text1_lines, text2_lines)
for line in diff:
sys.stdout.write(line)
输出:
***
---
***************
*** 41,54 ****
c e .- - D- i- f- f- e- r- e- n- c- e--- 41,43 ----
推荐阅读
- spring-boot - 将字段注入 JSON 响应对象
- exception - kotlin协程,launch如何处理异常
- scala - 从 ServerHealthKafka 或 ServerHealthSap 类型接收消息
- javascript - 如何将多维数组映射到html画布中的矩形网格?
- flutter - 在颤振构建之前运行自定义命令?
- javascript - 第二条条纹结帐按钮不起作用
- c# - 计算器测试,实现功能
- python - 使用 BeautifulSoup 提取 Indeed HTML 标签中的多个因子
- google-cloud-platform - 带有 Git 的 Google Kubernetes Engine CD CI
- c# - 将对象添加到 JSON 文件