Mokogwu Chiedu Mokogwu Chiedu - 1 month ago 11
Python Question

Extract data in a list surrounded by empty strings in python

I have a long list of strings with the useful data surrounded by empty strings like

['', '', '1', 'City', '10 (5 | 5)', '7 (3 | 4)', '2 (2 | 0)', '1 (0 | 1)', '24', '9', '15', '23', '', '', '2', 'nal', '10 (5 | 5)', '7 (3 | 4)', '2 (1 | 1)', '1 (1 | 0)', '23', '10', '13', '23', '', '', '3', 'pool', '10 (4 | 6)', '7 (3 | 4)', '2 (1 | 1)', '1 (0 | 1)', '24', '13', '11', '23', '', '',....]


I will like to extract the useful data to a list, for example the first item to extract will be

'1', 'City', '10 (5 | 5)', '7 (3 | 4)', '2 (2 | 0)', '1 (0 | 1)', '24', '9', '15', '23'


the second item to extract will be

'2', 'nal', '10 (5 | 5)', '7 (3 | 4)', '2 (1 | 1)', '1 (1 | 0)', '23', '10', '13', '23'


and so on

Answer

You can use groupby:

>>> from itertools import groupby
>>> d = ['', '', '1', 'City', '10 (5 | 5)', '7 (3 | 4)', '2 (2 | 0)', '1 (0 | 1)
', '24', '9', '15', '23', '', '', '2', 'nal', '10 (5 | 5)', '7 (3 | 4)', '2 (1 |
 1)', '1 (1 | 0)', '23', '10', '13', '23', '', '', '3', 'pool', '10 (4 | 6)', '7
 (3 | 4)', '2 (1 | 1)', '1 (0 | 1)', '24', '13', '11', '23', '', '']
>>> [list(g) for k, g in groupby(d, bool) if k]
[['1', 'City', '10 (5 | 5)', '7 (3 | 4)', '2 (2 | 0)', '1 (0 | 1)', '24', '9', '
15', '23'], ['2', 'nal', '10 (5 | 5)', '7 (3 | 4)', '2 (1 | 1)', '1 (1 | 0)', '2
3', '10', '13', '23'], ['3', 'pool', '10 (4 | 6)', '7 (3 | 4)', '2 (1 | 1)', '1
(0 | 1)', '24', '13', '11', '23']]

groupby groups the consecutive elements in the iterable based on the key function given as a second parameter. For each resulting group it returns tuple consisting of key value and iterator over the elements in the group.

In above bool is used as key function and it returns True for non-empty strings and False for empty ones. Then the final result is generated by discarding the groups with False key and converting the iterables to lists so that output can be printed.