简体   繁体   中英

flattening a dictionary containing a list of list and tuples

from numpy import nan
d = {'E1': (((((['C', 'Me', 'Lans'], float(nan), ['3050', '55901']),
     float(nan)),
    float(nan)),
   ['2011-09-05 00:00:00', '3050-09-02 00:00:00']),
  ['2011-09-05 00:00:00', '3050-09-02 00:00:00']),
 'E2': (((((['Can', 'Mar', 'Horns'], ['26D']),
      ['1001', '14086']),
     float(nan),
    ['(100) 300-2345']),
   ['1001-09-02 00:00:00', '0100-09-02 00:00:00', '2011-19-26 00:00:00']),
  ['1001-09-02 00:00:00', '0100-09-02 00:00:00', '2011-19-26 00:00:00'])}

I have a messy looking dict d . I would like the following output where d is a list within a dictionary

d = {'E1': ['C', 'Me', 'Lans', float(nan),'3050', '55901', float(nan)), float(nan), '2011-09-05 00:00:00', '3050-09-02 00:00:00',
            '2011-09-05 00:00:00', '3050-09-02 00:00:00'],
     'E2': ['Can', 'Mar', 'Horns', '26D','1001', '14086', float(nan),'100 300-2345','1001-09-02 00:00:00', '0100-09-02 00:00:00', '2011-19-26 00:00:00',
             '1001-09-02 00:00:00', '0100-09-02 00:00:00', '2011-19-26 00:00:00']}

I've tried the code Flatten nested dictionaries, compressing keys but it doesn't give me quite what I am looking for. Can any body help?

You can use a function that recursively flattens lists and tuples:

def flatten(o):
    return [s for i in o for s in flatten(i)] if isinstance(o, (list, tuple)) else [o]

so that {k: flatten(v) for k, v in d.items()} returns:

{'E1': ['C',
        'Me',
        'Lans',
        nan,
        '3050',
        '55901',
        nan,
        nan,
        '2011-09-05 00:00:00',
        '3050-09-02 00:00:00',
        '2011-09-05 00:00:00',
        '3050-09-02 00:00:00'],
 'E2': ['Can',
        'Mar',
        'Horns',
        '26D',
        '1001',
        '14086',
        nan,
        '(100) 300-2345',
        '1001-09-02 00:00:00',
        '0100-09-02 00:00:00',
        '2011-19-26 00:00:00',
        '1001-09-02 00:00:00',
        '0100-09-02 00:00:00',
        '2011-19-26 00:00:00']}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM