Skip to content

Latest commit

 

History

History
33 lines (25 loc) · 1.27 KB

File metadata and controls

33 lines (25 loc) · 1.27 KB

Sort Normalized Version Of Data

Let's say I have a list of names that I want to sort. However, because of inconsistency in how the data was entered, sometimes those names are capitalized and other times they are not. Using methodcaller, I can normalize the sorting key used when comparing list items.

First, let's look at calling sorted with the list and no key:

>>> sorted(["butler", "Jemisin", "le guin", "Erdrich"])
['Erdrich', 'Jemisin', 'butler', 'le guin']

butler which starts with a b gets moved to the 3rd position because it is lowercase.

To sort this list using a normalized comparison, we will use methodcaller to create a callable out of lower which is then passed as the sort key:

>>> from operator import methodcaller
>>> sorted(["butler", "Jemisin", "le guin", "Erdrich"], key=methodcaller("lower"))
['butler', 'Erdrich', 'Jemisin', 'le guin']

That's the sort order I was originally hoping for.

What methodcaller is doing is creating a callable function that will invoke lower with each string instance as the target. Conceptually similar to "Erdrich".lower() or even getattr("Erdrich", "lower")() (notice this needs to be immediately invoked).