Group a list of items according to the starting character(s) of items. This is based on Raymond Hettinger's groupby class: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259173
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | def groupbyhead(items,headsize=1):
'''
groupbyhead(items,headsize=1)
=============================
Group a list of items according to the starting character(s) of items.
Return a dictionary.
:Author: Runsun Pan
:Date: 1/1/06
:Required: Raymond Hettinger's groupby class
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259173
Usage
-----
::
>>> states = ['Alaska','Arizona','Arkansas','California','Colorado',
'Connecticut','Georgia','Idaho','Illinois','Indiana','Iowa','Kansas',
'Kentucky','Maine','Maryland', 'Michigan','Minnesota','Missouri','Montana']
>>> x= groupbyhead(states)
>>> print prndict(x, sortkey=1) #
{
'A':['Alaska', 'Arizona', 'Arkansas'],
'C':['California', 'Colorado', 'Connecticut'],
'G':['Georgia'],
'I':['Idaho', 'Illinois', 'Indiana', 'Iowa'],
'K':['Kansas', 'Kentucky'],
'M':['Maine', 'Maryland', 'Michigan', 'Minnesota', 'Missouri', 'Montana']
}
>>> x= groupbyhead(states,2)
>>> print prndict(x, sortkey=1) #
{
'Al':['Alaska'],
'Ar':['Arizona', 'Arkansas'],
'Ca':['California'],
'Co':['Colorado', 'Connecticut'],
'Ge':['Georgia'],
'Id':['Idaho'],
'Il':['Illinois'],
'In':['Indiana'],
'Io':['Iowa'],
'Ka':['Kansas'],
'Ke':['Kentucky'],
'Ma':['Maine', 'Maryland'],
'Mi':['Michigan', 'Minnesota', 'Missouri'],
'Mo':['Montana']
}
# prndict: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/327142
'''
return dict( [(k,g) for k, g in groupby(items, key=lambda x:x[0:headsize])] )
|
Tags: algorithms
Is there a reason you wouldn't just use vanilla list comprehensions to achieve:
Don't get me wrong, always nice to see difference solutions but these things are in Python to make it easier for everyone (and in this case, save about 10 lines of code)... :)
Hi Allan, surprised this is still alive :)
It's been so long that I can't recall the very reason for me to come up with this. Maybe I needed a table or something. I agree with you that a list comprehension is easier and more straightforward.