Finding differences between strings

2024/9/20 11:54:05

I have the following function that gets a source and a modified strings, and bolds the changed words in it.

def appendBoldChanges(s1, s2):"Adds <b></b> tags to words that are changed"l1 = s1.split(' ')l2 = s2.split(' ')for i, val in enumerate(l1):if l1[i].lower() != l2[i].lower():s2 = s2.replace(l2[i], "<b>%s</b>" % l2[i])return s2
print appendBoldChanges("britney spirs", "britney spears") # returns britney <b>spears</b>

It works fine on strings with the same word count, but fails with different word counts, as sora iro days and sorairo days.

How can I take the spaced into consideration?

Answer

You could use difflib, and do it like this:

from difflib import Differdef appendBoldChanges(s1, s2):"Adds <b></b> tags to words that are changed"l1 = s1.split(' ')l2 = s2.split(' ')dif = list(Differ().compare(l1, l2))return " ".join(['<b>'+i[2:]+'</b>' if i[:1] == '+' else i[2:] for i in dif if not i[:1] in '-?'])print appendBoldChanges("britney spirs", "britney sprears")
print appendBoldChanges("sora iro days", "sorairo days")
#Output:
britney <b>sprears</b>
<b>sorairo</b> days
https://en.xdnf.cn/q/72173.html

Related Q&A

Python pandas: select 2nd smallest value in groupby

I have an example DataFrame like the following:import pandas as pd import numpy as np df = pd.DataFrame({ID:[1,2,2,2,3,3,], date:array([2000-01-01,2002-01-01,2010-01-01,2003-01-01,2004-01-01,2008-01-01…

How to disable SSL3 and weak ciphers with cherrypy builtin ssl module (python 3)

I have configured Cherrypy 3.8.0 with Python 3 to use SSL/TLS. However, I want to disable SSL3 to avoid POODLE. I searched through the documentation but I am unsure on how to implement it.I am using th…

cleaning big data using python

I have to clean a input data file in python. Due to typo error, the datafield may have strings instead of numbers. I would like to identify all fields which are a string and fill these with NaN using p…

Using the Python shell in Vi mode on Windows

I know that you can use the Python shell in Vi mode on Unix-like operating systems. For example, I have this line in my ~/.inputrc:set editing-mode viThis lets me use Vi-style editing inside the Python…

Calculate residual deviance from scikit-learn logistic regression model

Is there any way to calculate residual deviance of a scikit-learn logistic regression model? This is a standard output from R model summaries, but I couldnt find it any of sklearns documentation.

Use Python to create 2D coordinate

I am truly a novice in Python. Now, I am doing a project which involves creating a list of 2D coordinates. The coordinates should be uniformly placed, using a square grid (10*10), like(0,0)(0,1)(0,2)(0…

How to pass Unicode title to matplotlib?

Cant get the titles right in matplotlib: technologien in C gives: technologien in CPossible solutions already tried:utechnologien in C doesnt work neither does: # -*- coding: utf-8 -*- at the beginnin…

Cythonize but not compile .pyx files using setup.py

I have a Cython project containing several .pyx files. To distribute my project I would like to provide my generated .c files as recommended in the Cython documentation, to minimize problems with diffe…

How to clear matplotlib labels in legend?

Is there a way to clear matplotlib labels inside a graphs legend? This post explains how to remove the legend itself, but the labels themselves still remain, and appear again if you plot a new figure.…

Threading and Signals problem in PyQt

Im having some problems with communicating between Threads in PyQt. Im using signals to communicate between two threads, a Sender and a Listener. The sender sends messages, which are expected to be rec…