1

I need to use or create a comparison function in Python, perhaps it already exists a way of doing this?

I need to compare a string with a value in a list, and I need to make a match even if it's a couple of characters off. I'll make an example so you can see what I mean.

Example 1:

Value in list : Name: This is the title

Value in search : Name This is the title

Example 2:

Value in list : Name and shortening m.m.

Value in search : Name and shortening m.m

As you can see the values I want to compare and need to match are very similar. The values in the search are folder names so they are a bit different because they contain illegal characters.

Maybe the easiest way to accomplish this is to remove none allowed characters from the string before making the comparison \/:*?"<>| and any trailing points.

Any tips on what's the most efficient way of comparing the strings and get a match is?

Edit: Is this an ugly way of doing it?

def Cleanup(Str):
    Illegal = ['\\','/',':','*','?','"','<','>','|']
    return ''.join([char for char in Str if char not in Illegal]).rstrip('.')
Jaman42
  • 98
  • 7

3 Answers3

0

I'm sure there's a better way to do this, but here's my crack at it

import string
a = "Name: This is the title"
b = "Name This is the title"

# remove punctuation and make all lower-case
def strippedAndLowered(myString):
    return "".join(i for i in myString if i not in string.punctuation).lower()

strippedAndLowered(a) == strippedAndLowered(b)  # returns True
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

Use the following code to strip the punctuation, and then compare them:

def search(lst, item):
    for i in lst:
            i = ''.join([char for char in i if char.isalpha() or char == ' '])
            if item == i:
                    return True
    return False
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • What exactly are you after, then? – A.J. Uppal Jun 03 '14 at 04:43
  • Sorry I removed my comment because I thought I misread your code. I didn't want to strip all punctuation, I edited my original post with at least one way of doing it to maybe clear things up. Thanks for posting – Jaman42 Jun 03 '14 at 05:37
0

The translate function should be faster:

item = "Name: This is the title"
search = "Name This is the title"
illegal = r'\/:*?"<>|'

def compare(s1, s2):
    return s1.translate(None, illegal) == s2.translate(None, illegal)

print compare(search, item)

Gives:

 True

And if you are really worried about the performance, and have many comparisons, you can cache the translated versions in a dictionary.

perreal
  • 94,503
  • 21
  • 155
  • 181