Python: How RECURSIVELY remove None values from a NESTED data structure (lists and dictionaries)?

后端 未结 5 1647
谎友^
谎友^ 2020-12-28 19:09

Here is some nested data, that includes lists, tuples, and dictionaries:

data1 = ( 501, (None, 999), None, (None), 504 )
data2 = { 1:601, 2:None, None:603, \         


        
5条回答
  •  遥遥无期
    2020-12-28 19:47

    If you can assume that the __init__ methods of the various subclasses have the same signature as the typical base class:

    def remove_none(obj):
      if isinstance(obj, (list, tuple, set)):
        return type(obj)(remove_none(x) for x in obj if x is not None)
      elif isinstance(obj, dict):
        return type(obj)((remove_none(k), remove_none(v))
          for k, v in obj.items() if k is not None and v is not None)
      else:
        return obj
    
    from collections import OrderedDict
    data1 = ( 501, (None, 999), None, (None), 504 )
    data2 = { 1:601, 2:None, None:603, 'four':'sixty' }
    data3 = OrderedDict( [(None, 401), (12, 402), (13, None), (14, data2)] )
    data = [ [None, 22, tuple([None]), (None,None), None], ( (None, 202), {None:301, 32:302, 33:data1}, data3 ) ]
    print remove_none(data)
    

    Note that this won't work with a defaultdict for example since the defaultdict takes and additional argument to __init__. To make it work with defaultdict would require another special case elif (before the one for regular dicts).


    Also note that I've actually constructed new objects. I haven't modified the old ones. It would be possible to modify the old objects if you didn't need to support modifying immutable objects like tuple.

提交回复
热议问题