Cleaner method for list comprehension clean-up

Posted by Dan McGrath on Stack Overflow See other posts from Stack Overflow or by Dan McGrath
Published on 2013-10-27T03:48:16Z Indexed on 2013/10/27 3:53 UTC
Read the original article Hit count: 160

Filed under:
|

This relates to my previous question: Converting from nested lists to a delimited string

I have an external service that sends data to us in a delimited string format. It is lists of items, up to 3 levels deep. Level 1 is delimited by '|'. Level 2 is delimited by ';' and level 3 is delimited by ','. Each level or element can have 0 or more items. An simplified example is:
a,b;c,d|e||f,g|h;;

We have a function that converts this to nested lists which is how it is manipulated in Python.

def dyn_to_lists(dyn):  
    return [[[c for c in b.split(',')] for b in a.split(';')] for a in dyn.split('|')]

For the example above, this function results in the following:

>>> dyn = "a,b;c,d|e||f,g|h;;"
>>> print (dyn_to_lists(dyn))
[[['a', 'b'], ['c', 'd']], [['e']], [['']], [['f', 'g']], [['h'], [''], ['']]]

For lists, at any level, with only one item, we want it as a scalar rather than a 1 item list. For lists that are empty, we want them as just an empty string. I've came up with this function, which does work:

def dyn_to_min_lists(dyn):
    def compress(x): 
        return "" if len(x) == 0 else x if len(x) != 1 else x[0]

    return compress([compress([compress([item for item in mv.split(',')]) for mv in attr.split(';')]) for attr in dyn.split('|')])

Using this function and using the example above, it returns:

[[['a', 'b'], ['c', 'd']], 'e', '', ['f', 'g'], ['h', '', '']]

Being new to Python, I'm not confident this is the best way to do it. Are there any cleaner ways to handle this?

This will potentially have large amounts of data passing through it, are there any more efficient/scalable ways to achieve this?

© Stack Overflow or respective owner

Related posts about python

Related posts about list-comprehension