Near Duplicate elements in List

Viewed 52
country_input = input('Search item: ') 
list_A = ['250177 Dominican Republic', '160 Dominica' ]
for items in list_A:
     val = items
     if any(country_input in val for items in list_A):
        print(items)
Search item: Dominica
250177 Dominican Republic
160 Dominica

Hi, the big problem is I am trying to retrieve Dominica only, but not Dominican Republic, because Dominican Republic and Dominica are near duplicates. It always retrieve Dominican Republic along with it, is there any fix to this

3 Answers

You can use the regular expression word boundaries to find the exact match.
Simply put: \b allows you to perform a whole words only search using a regular expression in the form of \bword\b.

import re

country_input = input("Search item: ")
list_A = ["250177 Dominican Republic", "160 Dominica"]

regex = re.compile(r"\b{}\b".format(country_input))
for item in list_A:
    if regex.search(item):
        print(item)

Output:

Search item: Dominica
160 Dominica

You are basically searching for the sub-string "country_input" in every item in the list, that's why you get both lines.

If the format is always the same (NUMBER(SPACE)NAME) you can split every item to remove the number and then search for exact match with "==":

for item in list_A:
  name = item.split(" ", 1)[1]
  if name == country_input:
    print(item)

There are many solutions based on what exactly you mean by near duplicate

Exact word/token match

country_input = "Dominica"
list_A = ['250177 Dominican Republic', '160 Dominica' ]

print ([txt for txt in list_A if country_input in txt.split()])

Exact case insensitive word/token match

print ([txt for txt in list_A if country_input.lower() in map(str.lower, txt.split())])

Fuzzy match

from fuzzywuzzy import fuzz
import numpy as np
country_input = "60 Dominica 12"
print (list_A[np.argsort(
    [fuzz.token_sort_ratio(country_input, txt) for txt in list_A])[-1]])
Related