6

This problem is most easily illustrated in pseudo-code. I have a list like this:

linelist = ["a", "b", "", "c", "d", "e", "", "a"]

I would like to get it in the format:

questionchunks = [["a", "b"], ["c", "d", "e"], ["a"]]

My first attempt is this:

questionchunks = []
qlist = []

for line in linelist:

    if (line != "" and len(qlist) != 0 ):
        questionchunks.append(qlist)
        qlist = []
    else: 
        qlist.append(line)

My output is a little messed up though. I'd be grateful for any pointers I can get.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
James
  • 3,957
  • 4
  • 37
  • 82
  • some of the answers rely on the array contents being contiguous, I suspect that isn't true in your real use case? – stevejpurves Jan 20 '15 at 16:34
  • @stevejpurves - The array contents will always be contiguous in that they will have groups of non-empty strings separated by empty strings. –  Jan 20 '15 at 16:44

2 Answers2

9

You are almost near your goal, this is the minimal edit required

linelist = ["a", "b", "", "c", "d", "e", "", "a"]
questionchunks = []
qlist = []
linelist.append('') # append an empty str at the end to avoid the other condn
for line in linelist:

    if (line != "" ):
        questionchunks.append(line)      # add the element to each of your chunk   
    else: 
        qlist.append(questionchunks)   # append chunk
        questionchunks = []       # reset chunk

print qlist
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • I am not sure what's OP desired output for something like: `["", "a", "b", "", "c", "d", "e", "", "a", ""]`, but this solution will add empty lists for this example. – Ashwini Chaudhary Jan 20 '15 at 16:33
  • @AshwiniChaudhary Thanks for that info. It does add empty lists, but isn't it just easy to remove them in one line ? Thanks again – Bhargav Rao Jan 20 '15 at 16:34
8

This can be easily done using itertools.groupby:

>>> from itertools import groupby
>>> linelist = ["a", "b", "", "c", "d", "e", "", "a"]
>>> split_at = ""
>>> [list(g) for k, g in groupby(linelist, lambda x: x != split_at) if k]
[['a', 'b'], ['c', 'd', 'e'], ['a']]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504