Python Forum
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find closest string pattern
#10
(Dec-12-2018, 08:54 PM)Gribouillis Wrote: Using the normalized compression distance, option 1 is better in the first example and option 2 is better in the second example
from zlib import compress

def ncd(a, b):
    """Normalized compression distance between two byte strings"""
    u =len(compress(a))
    v =len(compress(b))
    u, v =min(u, v), max (u, v)
    w =len(compress(a + b))
    return float (w - u) / v 

def l2b(alist):
    return ",".join(alist).encode()

def option_distances(alist, *options):
    b = l2b(alist)
    return [(ncd(b, l2b(option)), option)
            for option in options]


if __name__ == '__main__':
    my_list = ['one', 'two', 'three' , 'four']
    option_one = ['two', 'three', 'one', 'four']
    option_two = ['four', 'three', 'two', 'one']
    
    print(my_list)
    for d, option in option_distances(my_list, option_one, option_two):
        print(d, option)
    
    my_list = "red red red blue black".split()
    option1 = "blue red".split()
    option2 = "red black".split()
    
    print(my_list)
    for d, option in option_distances(my_list, option1, option2):
        print(d, option)
Output:
['one', 'two', 'three', 'four'] 0.19230769230769232 ['two', 'three', 'one', 'four'] 0.3076923076923077 ['four', 'three', 'two', 'one'] ['red', 'red', 'red', 'blue', 'black'] 0.43478260869565216 ['blue', 'red'] 0.391304347826087 ['red', 'black']

Perfect, thanks. I'll look at that. Appreciate the direction.
Reply


Messages In This Thread
Find closest string pattern - by jmair - Dec-12-2018, 07:07 PM
RE: Find closest string pattern - by micseydel - Dec-12-2018, 07:09 PM
RE: Find closest string pattern - by jmair - Dec-12-2018, 08:05 PM
RE: Find closest string pattern - by nilamo - Dec-12-2018, 07:37 PM
RE: Find closest string pattern - by jmair - Dec-12-2018, 07:49 PM
RE: Find closest string pattern - by nilamo - Dec-12-2018, 07:52 PM
RE: Find closest string pattern - by jmair - Dec-12-2018, 08:14 PM
RE: Find closest string pattern - by Gribouillis - Dec-12-2018, 08:11 PM
RE: Find closest string pattern - by Gribouillis - Dec-12-2018, 08:54 PM
RE: Find closest string pattern - by jmair - Dec-12-2018, 09:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Find and Replace numbers in String giddyhead 2 1,244 Jul-17-2022, 06:22 PM
Last Post: giddyhead
Thumbs Up [SOLVED] Find last occurence of pattern in text file? Winfried 4 4,412 Aug-13-2021, 08:21 PM
Last Post: Winfried
  Find string between two substrings, in a stream of data xbit 1 2,162 May-09-2021, 03:32 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,427 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  find a string in a field in MongoDB Leon79 2 2,444 Jul-19-2020, 09:20 PM
Last Post: menator01
  Help to find a string and read the next lines crlamaral 4 2,483 Mar-19-2020, 09:24 AM
Last Post: Larz60+
  How To Find an Opening and Closing String, Copying Open/Close/Contents to New File davidshq 1 2,044 Mar-03-2020, 04:47 AM
Last Post: davidshq
  'Get closest value array in array of arrays.' follow up help. DreamingInsanity 10 7,303 Dec-05-2019, 06:30 PM
Last Post: DreamingInsanity
  Get closest value array for array of arrays. DreamingInsanity 2 2,346 Nov-18-2019, 03:55 PM
Last Post: DreamingInsanity
  finding the closest floating point number in a list Skaperen 17 8,303 Sep-19-2019, 10:39 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020