I'm working a on a list like this, a = ['a','b','','','c','d'], the real list is including thousands of data entries. Is there a fancy way to make the list a as [['a','b'],['c','d]] because the data is really huge?
-
1What should the output look like if there was a single (or an odd number) of blank strings in a row?Gerrat– Gerrat2014-11-11 22:20:45 +00:00Commented Nov 11, 2014 at 22:20
-
if there is a single blank string like a = ['a','','b','','','c','d'], I want [['a','','b'],['c','d]] Thank you so muchkitty– kitty2014-11-11 22:35:32 +00:00Commented Nov 11, 2014 at 22:35
-
1Now I have to ask about what to do for 3 (or more) blank strings in a row, since your single blank example doesn't really explain enough.Gerrat– Gerrat2014-11-11 23:05:04 +00:00Commented Nov 11, 2014 at 23:05
-
What should:['a','b','','c','d'] look like? What about: ['a','','','',b','','c','d']? Maybe you can add examples, and explain better exactly what to do with blanks.Gerrat– Gerrat2014-11-11 23:23:27 +00:00Commented Nov 11, 2014 at 23:23
Add a comment
|
1 Answer
You can use itertools.groupby for this. You basically group by consecutive empty strings, or consecutive non-empty strings. Then keep all groups that were grouped by True from the lambda in a list comprehension.
>>> from itertools import groupby
>>> [list(i[1]) for i in groupby(a, lambda i: i != '') if i[0]]
[['a', 'b'], ['c', 'd']]
For another example
>>> b = ['a','b','','','c','d', '', 'e', 'f', 'g', '', '', 'h']
>>> [list(i[1]) for i in groupby(b, lambda i: i != '') if i[0]]
[['a', 'b'], ['c', 'd'], ['e', 'f', 'g'], ['h']]
3 Comments
abarnert
In this case, you could use
bool instead of lambda i: i != '', in the same way you'd normally write if i: instead of if i != '':. I'm not sure whether that would be more readable or less in this case—it makes it less explicit that you're dealing with strings, but it also cuts a lot of irrelevant boilerplate out of the expression, so… I guess just try it both ways and see which seems more readable to you.abarnert
Also, I think it might be more readable as
[list(g) for k, g in groupby(…)], but that's really nitpicky, so feel free to ignore it.kitty
Thank you guys so much! But I don't want it separate when there is only one blank string. like a = ['a','','b','','','c','d'], what I expect is [['a','','b'],['c','d]].