Python Programming:
Given the following code:
____________________________________
from difflib import SequenceMatcher
def getRatio(a,b):
lower_a = a.lower()
lower_b = b.lower()
return SequenceMatcher(None,
lower_a,lower_b).ratio()
myList = ['Hello1','hello2', 'hi!!!', 'hI!!!','kop','howareyou',
'howareYou2']
newList = []
____________________________________
The getRatio() method check the similarity of two strings. If
they are 100% similar, it return a ratio of 1.0. If they don't
match at all, ratio is 0.0, and if they are 80% similar, it returns
ratio 0.8 and so on.
First, append the first word of myList to newList. Then,
itterate over myList from index 1 to the end: if the word is more
than 80% similar to one of the words existing in newList (ratio
>= 0.8), append that same word that exists in newList (copy it).
and if the word has less than 0.8 ratio with all words
existing in newList, append that word from myList to newList.
The final output should look like this:
myList = ['Hello1','hello2', 'hi!!!', 'hI!!!','kop',
'howareyou', 'howareYou2']
newList = ['Hello1', 'Hello1', 'hi!!!', 'hi!!!', 'kop',
''howareyou', 'howareyou']
NB: PLEASE NOTE that the LENGTH of myList and newList should
always be the same.
Python Programming: Given the following code: ____________________________________ from difflib import SequenceMatcher d
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Python Programming: Given the following code: ____________________________________ from difflib import SequenceMatcher d
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!