# Posted By

gdvickery on 01/11/10

# Statistics

Viewed 499 times
Favorited by 0 user(s)

# Sort List By Group

/ Published in: Python
Save to your folder(s)

This function takes a list of string and sorts them based on their similarity. The percent at which they match can be defined in the second parameter (default 75%).

Copy this code and paste it in your HTML
`def sortByGroup(lst, percent = 75):    groups = []    for item in lst:        match = False         for g in xrange(len(groups)):            group = groups[g]            parent = group[0]            points = 0.0             try:                for x in xrange(len(parent)):                    if parent[x] == item[x]:                        points += 1                 if (points / len(parent)) * 100 >= percent:                    group.append(item)                    group.sort()                    match = True            except:                pass         if not match:               groups.append([item])     return groups # Example:random = [    'bob1',    'frank2',    'bob3',    'joe2',    'frank1',    'bob2',    'joe1',    'joe3']groups = sortByGroup(random)for g in groups:    for i in g:        print i    print '-' * 30 # Example Output:bob1bob2bob3------------------------------frank1frank2------------------------------joe1joe2joe3------------------------------`