开发者

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

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜