1

I have a string and a list of strings. I just want to know which text in the list is 100% partially matched with the given string.

from fuzzywuzzy import fuzz 

s1 = "Hello"
s_list= ["Hai all", "Hello world", "Thank you"]

fuzz.partial_ratio(s1, s_list)

For this am getting 100. Since "Hello" has a partial match with "Hello world" But how can I get "Hello World" as output?

Could anyone help me with this? Thanks in advance.

2 Answers2

1

You can use a different function

from fuzzywuzzy import process

s1 = "Hello"
s_list= ["Hai all", "Hello world", "Thank you"]

[s for s, m in process.extract(s1, s_list) if m == 100]

For more info check help(process.extract). If you strictly want the 100% partial matches, Neil's answer is better.

kuco 23
  • 786
  • 5
  • 18
  • This is drastically more expensive than using "in". – Neil Oct 07 '20 at 08:08
  • This also will only return "Hello world" even if there is another match, eg "Hello there" in the list as well. – Neil Oct 07 '20 at 08:11
  • @Neil I assumed that OP wanted to use this library, though I can see it's not his priority. – kuco 23 Oct 07 '20 at 08:12
  • Thank you @kuco23 and Neil for your answers. My intention is to get a 100% partial match, since am tried fuzzy-wuzzy I posted and stuck there I posted in that manner. But Neil's answer suits better for my requirement. Thank you Neil. :) – KV KRISHNASHAI Oct 07 '20 at 12:25
0

You do not need fuzzywuzzy for exact matching. Fuzzywuzzy is for fuzzy matching. Fuzzywuzzy cannot produce indexes for matches precisely because, in general, there is no "match", just distances.

All you need is Python.

s1 = "Hello"
s_list= ["Hai all", "Hello world", "Thank you"]

for item in s_list:
    if s1 in item:
        print("item: " + item + "\ns1" + s1)
Neil
  • 3,020
  • 4
  • 25
  • 48