从具有“nan”值的字典中删除键

时间:2021-06-07 07:20:31

标签: python

我有源词典,请参考下面的示例。它具有深层嵌套的子字典和列表,我需要删除值为“nan”的所有键。

data = {"col1":"val1","col2":"val2","col3":"val3","col4":"val3","list1":[{"l1":"v1","l2":"nan"},{"K1":"Kv1","K2":"nan"},{"M1":"Mv1","M2":"nan","sublist1":[{"SL1":"SV1","SL2":"nan"}]}],"list2":[{"l1":"v1","l2":"nan"},{"K1":"Kv1","K2":"nan"},{"M1":"Mv1","M2":"nan","sublist2":[{"SL1":"SV1","SL2":"nan"}]}]}

我通过创建一个函数尝试了以下代码,但它没有按预期工作:

def cleanNullTerms(d): 
   clean = {}
   for k, v in d.items():
      if isinstance(v, list):
         for values in v:
            nested = cleanNullTerms(values)
            if values =='nan':
                clean[k] = nested
      elif v is not 'nan':
         clean[k] = v
   return clean

2 个答案:

答案 0 :(得分:0)

您的代码将列表的值放在列表所在的位置。如果有很多值,每个值都会覆盖最后一个,所以只有最后一个被保存。它并没有被递归地收录到词典中。如果它们与字典和列表嵌套,它仍然只删除“nan”。例如,它不会进入集合。如果你想让它进入集合,写一个评论,我可以添加这个。

def cleanNullTerms(d):
   clean={}
   for k, v in d.items():
      if isinstance(v, list):
         v2=[]
         for value0 in v:
            nested=cleanNullTerms(value0)
            if value0!='nan':
                v2.append(nested)
         clean[k]=v2
      elif isinstance(v, dict):
          clean[k]=cleanNullTerms(v)
      elif v!='nan':
         clean[k]=v
   return clean

答案 1 :(得分:0)

您没有准确描述问题所在,但从检查来看,您的函数在我看来显然是错误的,因为它没有检查嵌套的 dict。您也没有说明您的预期输出是什么,但以下内容在我看来是正确的。

from pprint import pprint, pp


def cleanNullTerms(d):
    clean = {}
    for k, v in d.items():
        if isinstance(v, dict):
            clean[k] = cleanNullTerms(v)
        elif isinstance(v, list):
            clean[k] = [cleanNullTerms(values) for values in v]
        elif v != 'nan':
            clean[k] = v
    return clean


data = {'col1': 'val1',
        'col2': 'val2',
        'col3': 'val3',
        'col4': 'val3',
        'list1': [{'l1': 'v1', 'l2': 'nan'},
                  {'K1': 'Kv1', 'K2': 'nan'},
                  {'M1': 'Mv1',
                   'M2': 'nan',
                   'sublist1': [{'SL1': 'SV1', 'SL2': 'nan'}]}],
        'list2': [{'l1': 'v1', 'l2': 'nan'},
                  {'K1': 'Kv1', 'K2': 'nan'},
                  {'M1': 'Mv1',
                   'M2': 'nan',
                   'sublist2': [{'SL1': 'SV1', 'SL2': 'nan'}]}]}

data = cleanNullTerms(data)
pprint(data, sort_dicts=False)

打印结果:

{'col1': 'val1',
 'col2': 'val2',
 'col3': 'val3',
 'col4': 'val3',
 'list1': [{'l1': 'v1'},
           {'K1': 'Kv1'},
           {'M1': 'Mv1', 'sublist1': [{'SL1': 'SV1'}]}],
 'list2': [{'l1': 'v1'},
           {'K1': 'Kv1'},
           {'M1': 'Mv1', 'sublist2': [{'SL1': 'SV1'}]}]}