Merge lp:~john-turek/python-snippets/jtbranch into lp:~jonobacon/python-snippets/trunk

Proposed by John Turek
Status: Merged
Merged at revision: not available
Proposed branch: lp:~john-turek/python-snippets/jtbranch
Merge into: lp:~jonobacon/python-snippets/trunk
Diff against target: 55 lines (+51/-0)
1 file modified
pythoncore/sortlistbygroup.py (+51/-0)
To merge this branch: bzr merge lp:~john-turek/python-snippets/jtbranch
Reviewer Review Type Date Requested Status
Jono Bacon Approve
Review via email: mp+17255@code.launchpad.net
To post a comment you must log in.
Revision history for this message
John Turek (john-turek) wrote :

This is my first commit ever to launchpad (and first ever code commit on bzr ever!) I am going to put together many more of these, but wanted to make sure I got the process right. Thanks Jono!

Revision history for this message
Jono Bacon (jonobacon) wrote :

Thanks, John, looks good! The only change I made was that the category is not 'pythoncore' but 'Python Core'. Other than that, keep 'em coming!

review: Approve
15. By Jono Bacon

Sort List By Group snippet. Thanks, John!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'pythoncore/sortlistbygroup.py'
2--- pythoncore/sortlistbygroup.py 1970-01-01 00:00:00 +0000
3+++ pythoncore/sortlistbygroup.py 2010-01-12 21:31:12 +0000
4@@ -0,0 +1,51 @@
5+# [SNIPPET_NAME: Sort list by group]
6+# [SNIPPET_CATEGORIES: pythoncore]
7+# [SNIPPET_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%)]
8+# [SNIPPET_AUTHOR: John Turek <jt@dweeb.net>]
9+# [SNIPPET_LICENSE: GPL]
10+
11+def sortByGroup(lst, percent = 75):
12+ groups = []
13+ for item in lst:
14+ match = False
15+
16+ for g in xrange(len(groups)):
17+ group = groups[g]
18+ parent = group[0]
19+ points = 0.0
20+
21+ try:
22+ for x in xrange(len(parent)):
23+ if parent[x] == item[x]:
24+ points += 1
25+
26+ if (points / len(parent)) * 100 >= percent:
27+ group.append(item)
28+ group.sort()
29+ match = True
30+ except:
31+ pass
32+
33+ if not match:
34+ groups.append([item])
35+
36+ return groups
37+
38+# Example:
39+random = [
40+ 'bob1',
41+ 'frank2',
42+ 'bob3',
43+ 'joe2',
44+ 'frank1',
45+ 'bob2',
46+ 'joe1',
47+ 'joe3'
48+]
49+groups = sortByGroup(random)
50+for g in groups:
51+ for i in g:
52+ print i
53+ print '-' * 30
54+
55+

Subscribers

People subscribed via source and target branches