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

Subscribers

People subscribed via source and target branches