Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group items in an iterable object based on the first character of the item?

Tags:

python

Starting with a sorted iterable object I need to group the items by their first character (say a group for every letter from a to z and a group for numbers and symbols).

For a more concrete example, let's say I have this list:

L = ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc', '10', '%a', ' b', ...]

And I need something like:

GL = [['aa', 'ab', 'ac'], ['ba', 'bb', 'bc'], ['ca', 'cb', 'cc'], ['10', '%a', ' b']]

What are the options for doing so, and which is the most efficient?

like image 886
andreliebschner Avatar asked Jan 19 '23 20:01

andreliebschner


1 Answers

import itertools as it

L = ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc', '10', '%a', ' b']

sorter = lambda x: x[0].lower() if x and x[0].isalpha() else '}'
GL = [list(v) for k, v in it.groupby(sorted(L, key=sorter), key=sorter)]

returns:

[['aa', 'ab', 'ac'],
 ['ba', 'bb', 'bc'],
 ['ca', 'cb', 'cc'],
 ['10', '%a', ' b']]

You can use something else instead of '}' to put all non-alpha-characters at the correct position in the sorting.

like image 119
eumiro Avatar answered Mar 29 '23 23:03

eumiro