Return to Snippet

Revision: 22394
at January 11, 2010 16:59 by gdvickery


Initial Code
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:
bob1
bob2
bob3
------------------------------
frank1
frank2
------------------------------
joe1
joe2
joe3
------------------------------

Initial URL

                                

Initial Description
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%).

Initial Title
Sort List By Group

Initial Tags
sort, list, python

Initial Language
Python