Welcome, guest | Sign In | My Account | Store | Cart

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

Python, 53 lines
 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])] )

2 comments

Allan Mullan 10 years, 1 month ago  # | flag

Is there a reason you wouldn't just use vanilla list comprehensions to achieve:

states = ['Alaska','Arizona','Arkansas','California','Colorado',
    'Connecticut','Georgia','Idaho','Illinois','Indiana','Iowa','Kansas',
    'Kentucky','Maine','Maryland', 'Michigan','Minnesota','Missouri','Montana']

print [x for x in letters if x.startswith('Ar')]

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)... :)

runsun pan (author) 10 years, 1 month ago  # | flag

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.

Created by runsun pan on Tue, 3 Jan 2006 (PSF)
Python recipes (4591)
runsun pan's recipes (5)

Required Modules

  • (none specified)

Other Information and Tasks