Rav B
Rav B

Reputation: 11

use custom compare function in set equality python

I wish to use a custom compare function while calculating set. I wish to take advantage of the efficiencies of using set algorithm. technically I could create a double for loop to compare the two lists (keep, original) but I thought this might not be efficient.

eg://

textlist = ["ravi is happy", "happy ravi is", "is happy ravi", "is ravi happy"]

set() should return only 1 of these elements as the compare function would return if True if similarity between comparing items >= threshold.

In python. Thanks.

P.S.

The real trick is that I'd like to use my string_compare(t1,t2): Float to do the comparison rather then hashing and equal...

P.S.S.

C# has similar function: How to remove similar string from a list?

Upvotes: 0

Views: 314

Answers (1)

Issei Kumagai
Issei Kumagai

Reputation: 685

I think this is what you were looking for:

{' '.join(sorted(sentence.split())) for sentence in textlist}

This re-orders the string and therefore Python set will now work because we are comparing identical strings.

Upvotes: 1

Related Questions