1

I have

target = "dexter.new.blood.S01EP07.somerandomstring.mkv"

I managed to get only the name using split('.') to be

target = "dexternewblood"

and

list = ['Dexter - Eighth Season (2013)', 'Dexter - Seventh Season  (2012)', 'Dexter - Fourth Season (2009)', 'Dexter - Third Season (2008)', 'Dexter - Sixth Season (2011)', 'Dexter - Fifth Season (2010)', 'Dexter: New Blood - First Season (2021)', 'Dexter - First Season (2006)', 'Dexter - Second Season (2007)']

I did the same thing to end up with:

list = ['Dexter ', 'Dexter ', 'Dexter ', 'Dexter ', 'Dexter ', 'Dexter ', 'Dexter: New Blood ', 'Dexter ', 'Dexter ']

I wanna get the best match which is "Dexter: New Blood" if you can add way for getting index as well

Pedro Maia
  • 2,666
  • 1
  • 5
  • 20
  • 5
    Welcome to StackOverflow! Can you rigorously define what you mean by "best match"? What have you tried so far, and what isn't working about what you have tried? – BrokenBenchmark Dec 28 '21 at 20:31
  • 1
    "I wanna get the best match which is "Dexter: New Blood"" **Why** is this the best match? What is the rule that tells you so? If I say that the best match is instead "Dexter ", why am I wrong? – Karl Knechtel Dec 28 '21 at 20:37
  • 1
    @Khalid-elhirche As others have said, you have to say **exactly** what you mean by "best match" (i.e. a rule for saying whether one match is better than the other) for us to be able to help you. For example, perhaps you could use the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) to say how good a match is. – Ben Grossmann Dec 28 '21 at 20:42
  • i want based on the target like what's the close match to target – Khalid -elhirche Dec 28 '21 at 20:43
  • @Khalid-elhirche What does "close" mean? – Ben Grossmann Dec 28 '21 at 20:43
  • @Khalid-elhirche How exactly would a computer be able to figure out that "Dexter: New Blood" is "closer" than just "Dexter"? – Ben Grossmann Dec 28 '21 at 20:45
  • target = "dexternewblood" and my goal is to get "Dexter: New Blood",bcuz it's more close to target ,sorry idk how to explain it. – Khalid -elhirche Dec 28 '21 at 20:46

1 Answers1

2

From this similar post you can use SequenceMatcher from difflib built-in module:

from difflib import SequenceMatcher

target = "dexternewblood"
lst = ['Dexter ', 'Dexter ', 'Dexter ', 'Dexter ', 'Dexter ', 'Dexter ', 'Dexter: New Blood ', 'Dexter ', 'Dexter ']

a = [SequenceMatcher(None, i, target).ratio() for i in lst]

index = a.index(max(a)) # 6
match = lst[index] # 'Dexter: New Blood '
Pedro Maia
  • 2,666
  • 1
  • 5
  • 20