【问题标题】:How to print out the differences between two text (Python)如何打印出两个文本之间的差异(Python)
【发布时间】:2015-11-02 12:39:43
【问题描述】:

我的代码是这样的:

text1 = "I like apple"
text2 = "I like car"
if text1 != text2:
   print "the differences are:"the_difference

输出应该是:

the differences are:car


感谢阅读

【问题讨论】:

  • 如果 text2 是 text2 = "An apple I like"。它应该打印the differences are: an apple吗?
  • 在空格上拆分两个句子,然后将一个列表与另一个列表进行比较
  • @Matheus208 这就是我的意思
  • @ZeroX 只是想知道。如果顺序很重要,那么解决方案就会不同。
  • 如果正如@Matheus208 所说,那么答案可能有点困难......也许有一个模块可以做到这一点。

标签: python difference


【解决方案1】:
text1 = "I like apple"
text2 = "I like car"
set1 = set(text1.split())
set2 = set(text2.split())
diffs = set1.symmetric_difference(set2)

【讨论】:

  • @ZeroX 但是如果text2 是text2 = "An apple I like"。这不打印the differences are: an apple,它只打印{'An'}。
  • @KevinGuan 是的,这就是我想要的
猜你喜欢
  • 2014-04-28
  • 2018-09-23
  • 1970-01-01
  • 2021-05-28
  • 1970-01-01
  • 2016-12-24
  • 2014-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多