Created
May 29, 2010 10:49
-
-
Save malero/418204 to your computer and use it in GitHub Desktop.
Revisions
-
Matt revised this gist
Feb 14, 2011 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ from operator import itemgetter def multikeysort(items, columns, functions={}, getter=itemgetter): """Sort a list of dictionary objects or objects by multiple keys bidirectionally. Keyword Arguments: @@ -9,7 +9,6 @@ def multikeysort(items, columns, functions={}, getter=itemgetter, cmp=cmp): getter -- Default "getter" if column function does not exist operator.itemgetter for Dictionaries operator.attrgetter for Objects """ comparers = [] for col in columns: -
malero revised this gist
Jun 9, 2010 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ from operator import itemgetter def multikeysort(items, columns, functions={}, getter=itemgetter, cmp=cmp): """Sort a list of dictionary objects or objects by multiple keys bidirectionally. Keyword Arguments: @@ -9,6 +9,7 @@ def multikeysort(items, columns, functions={}, getter=itemgetter): getter -- Default "getter" if column function does not exist operator.itemgetter for Dictionaries operator.attrgetter for Objects cmp -- The compare function to use """ comparers = [] for col in columns: -
malero revised this gist
May 29, 2010 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ # Sort name alphabetically and age in descending order mylist = sorted(mylist, key=lambda k: (k['name'].lower(), -k['age'])) -
malero revised this gist
May 29, 2010 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ # Sort by multiple keys - case sensitive from operator import itemgetter mylist = sorted(mylist, key=itemgetter('name', 'age')) # Sort by multiple keys - case insensitive mylist = sorted(mylist, key=lambda k: (k['name'].lower(), k['age'])) -
malero revised this gist
May 29, 2010 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ # Sort a list of dictionary objects by a key - case sensitive from operator import itemgetter mylist = sorted(mylist, key=itemgetter('name')) # Sort a list of dictionary objects by a key - case insensitive mylist = sorted(mylist, key=lambda k: k['name'].lower()) -
malero revised this gist
May 29, 2010 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ # Sort by class and grade descending, case insensitive from operator import itemgetter, methodcaller # Create a unary function that will get the column value and then call the .lower() method get_class = compose(itemgetter('class'), methodcaller('lower')) get_grade = compose(itemgetter('grade'), methodcaller('lower')) -
malero revised this gist
May 29, 2010 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,4 @@ get_class = compose(itemgetter('class'), methodcaller('lower')) get_grade = compose(itemgetter('grade'), methodcaller('lower')) ex1 = multikeysort(students, ['class', '-grade'],{'class':get_class,'grade':get_grade}) -
malero revised this gist
May 29, 2010 . 1 changed file with 23 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ # We will use a list of dictionary objects representing students, their classes and their current grade students = [ {'name':'Paul Allen', 'class':'Science','grade':'A'}, {'name':'paul allen', 'class':'Math','grade':'C'}, {'name':'Bob Lewis', 'class':'Science','grade':'D'}, {'name':'Bob Lewis', 'class':'math','grade':'b'}, {'name':'bob Lewis', 'class':'History','grade':'f'}, ] # Sort by class and grade descending, case sensitive ex1 = multikeysort(students, ['class', '-grade']) # Sort by class and grade descending, case insensitive from operator import itemgetter, methodcaller # Create a unary function that will get the column value and then call the .lower() method on the value get_class = compose(itemgetter('class'), methodcaller('lower')) get_grade = compose(itemgetter('grade'), methodcaller('lower')) ex1 = multikeysort(students, ['class', '-grade'],{'class':get_class,'grade':get_grade}) -
malero revised this gist
May 29, 2010 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,6 +27,7 @@ def comparer(left, right): return sorted(items, cmp=comparer) def compose(inner_func, *outer_funcs): """Compose multiple unary functions together into a single unary function""" if not outer_funcs: return inner_func outer_func = compose(*outer_funcs) -
malero revised this gist
May 29, 2010 . 2 changed files with 3 additions and 1 deletion.There are no files selected for viewing
Empty file.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,9 @@ def multikeysort(items, columns, functions={}, getter=itemgetter): items -- A list of dictionary objects or objects columns -- A list of column names to sort by. Use -column to sort in descending order functions -- A Dictionary of Column Name -> Functions to normalize or process each column value getter -- Default "getter" if column function does not exist operator.itemgetter for Dictionaries operator.attrgetter for Objects """ comparers = [] for col in columns: -
malero revised this gist
May 29, 2010 . 1 changed file with 0 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,10 +7,6 @@ def multikeysort(items, columns, functions={}, getter=itemgetter): columns -- A list of column names to sort by. Use -column to sort in descending order functions -- A Dictionary of Column Name -> Functions to normalize or process each column value getter -- operator.itemgetter for Dictionaries, operator.attrgetter for Objects """ comparers = [] for col in columns: -
malero revised this gist
May 29, 2010 . 2 changed files with 12 additions and 0 deletions.There are no files selected for viewing
Empty file.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,17 @@ from operator import itemgetter def multikeysort(items, columns, functions={}, getter=itemgetter): """Sort a list of dictionary objects or objects by multiple keys bidirectionally. Keyword Arguments: items -- A list of dictionary objects or objects columns -- A list of column names to sort by. Use -column to sort in descending order functions -- A Dictionary of Column Name -> Functions to normalize or process each column value getter -- operator.itemgetter for Dictionaries, operator.attrgetter for Objects Example: li = [{'fname':'Bob','lname':'Lewis'},{'fname':'bob','lname':'Louis'}] li = multikeysort(li, ['-lname', 'fname']) """ comparers = [] for col in columns: column = col[1:] if col.startswith('-') else col -
malero created this gist
May 29, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ from operator import itemgetter def multikeysort(items, columns, functions={}, getter=itemgetter): comparers = [] for col in columns: column = col[1:] if col.startswith('-') else col if not column in functions: functions[column] = getter(column) comparers.append((functions[column], 1 if column == col else -1)) def comparer(left, right): for func, polarity in comparers: result = cmp(func(left), func(right)) if result: return polarity * result else: return 0 return sorted(items, cmp=comparer) def compose(inner_func, *outer_funcs): if not outer_funcs: return inner_func outer_func = compose(*outer_funcs) return lambda *args, **kwargs: outer_func(inner_func(*args, **kwargs))