Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a list by first character of each element

I have a Python list mylist whose elements are a sublist containing a string of a letter and number. I was wondering how I could split mylist by the character at the start of the string without using code with individual statements/cases for each character. Say I want to split mylist into lists a, b, c:

mylist = [['a1'],['a2'],['c1'],['b1']]
a = [['a1'],['a2']]
b = [['b1']]
c = [['c1']]

It is important that I keep them as a list-of-lists (even though it's only a single element in each sublist).

like image 278
user1392103 Avatar asked May 13 '12 11:05

user1392103


1 Answers

This will work:

import itertools as it

mylist  = [['a1'],['a2'],['c1'],['b1']]
keyfunc = lambda x: x[0][0]

mylist = sorted(mylist, key=keyfunc)
a, b, c = [list(g) for k, g in it.groupby(mylist, keyfunc)]

The line where sorted() is used is necessary only if the elements in mylist are not already sorted by the character at the start of the string.

EDIT :

As pointed out in the comments, a more general solution (one that does not restrict the number of variables to just three) would be using dictionary comprehensions (available in Python 2.7+) like this:

result_dict = {k: list(g) for k, g in it.groupby(mylist, keyfunc)}

Now the answer is keyed in the dictionary by the first character:

result_dict['a']
> [['a1'],['a2']]

result_dict['b']
> [['b1']]

result_dict['c']
> [['c1']]
like image 150
Óscar López Avatar answered Oct 28 '22 22:10

Óscar López