以下是一些嵌套数据,包括列表,元组和词典:
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 ) ]
目标:删除任何无键的键或值(来自“数据”)。如果列表或字典包含一个值,那么它本身就是一个列表,元组或字典,然后是RECURSE,以删除NESTED Nones。
期望的输出:
[[22, (), ()], ((202,), {32: 302, 33: (501, (999,), 504)}, OrderedDict([(12, 402), (14, {'four': 'sixty', 1: 601})]))]
或者更可读的是,这里是格式化输出:
StripNones(data)= list:
. [22, (), ()]
. tuple:
. . (202,)
. . {32: 302, 33: (501, (999,), 504)}
. . OrderedDict([(12, 402), (14, {'four': 'sixty', 1: 601})])
我将提出一个可能的答案,因为我还没有找到解决方案。我感谢任何替代方案或对现有解决方案的指示。
修改 我忘了提到这必须在Python 2.7中工作。我现在不能使用Python 3。
虽然 IS 值得为其他人发布Python 3解决方案。所以请说明你要回答哪个python。
答案 0 :(得分:13)
如果您可以假设各个子类的__init__
方法与典型基类具有相同的签名:
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)
请注意,此不会使用defaultdict
,因为defaultdict需要__init__
的附加参数。要使其与defaultdict
一起使用,需要另一个特殊情况elif
(在常规字典之前)。
另请注意,我实际构建了 new 对象。我没有修改旧的。如果您不需要支持修改tuple
等不可变对象,则可以修改旧对象。
答案 1 :(得分:11)
如果你想要一个全功能但简洁的方法来处理像这样的现实世界的嵌套数据结构,甚至处理周期,我建议你看看the remap utility from the boltons utility package。
在pip install boltons
或将iterutils.py复制到项目后,只需执行以下操作:
from collections import OrderedDict
from boltons.iterutils import remap
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 ) ]
drop_none = lambda path, key, value: key is not None and value is not None
cleaned = remap(data, visit=drop_none)
print(cleaned)
# got:
[[22, (), ()], ((202,), {32: 302, 33: (501, (999,), 504)}, OrderedDict([(12, 402), (14, {'four': 'sixty', 1: 601})]))]
This page has many more examples,包括使用更大对象的人(来自Github的API)。
它是纯Python,因此它可以在任何地方使用,并且在Python 2.7和3.3+中进行了全面测试。最重要的是,我为这样的情况编写了它,所以如果你找到一个它无法处理的案例,你可以让我解决它right here。
答案 2 :(得分:6)
def stripNone(data):
if isinstance(data, dict):
return {k:stripNone(v) for k, v in data.items() if k is not None and v is not None}
elif isinstance(data, list):
return [stripNone(item) for item in data if item is not None]
elif isinstance(data, tuple):
return tuple(stripNone(item) for item in data if item is not None)
elif isinstance(data, set):
return {stripNone(item) for item in data if item is not None}
else:
return data
示例运行
print stripNone(data1)
print stripNone(data2)
print stripNone(data3)
print stripNone(data)
(501, (999,), 504)
{'four': 'sixty', 1: 601}
{12: 402, 14: {'four': 'sixty', 1: 601}}
[[22, (), ()], ((202,), {32: 302, 33: (501, (999,), 504)}, {12: 402, 14: {'four': 'sixty', 1: 601}})]
答案 3 :(得分:2)
def purify(o):
if hasattr(o, 'items'):
oo = type(o)()
for k in o:
if k != None and o[k] != None:
oo[k] = purify(o[k])
elif hasattr(o, '__iter__'):
oo = [ ]
for it in o:
if it != None:
oo.append(purify(it))
else: return o
return type(o)(oo)
print purify(data)
给出:
[[22, (), ()], ((202,), {32: 302, 33: (501, (999,), 504)}, OrderedDict([(12, 402), (14, {'four': 'sixty', 1: 601})]))]
答案 4 :(得分:0)
在发布问题之前,这是我最初的尝试。 保持在这里,因为它可能有助于解释目标。
如果想要修改现有的LARGE集合,而不是将数据复制到NEW集合中,它还有一些有用的代码。 (其他答案创建新的集合。)
# ---------- StripNones.py Python 2.7 ----------
import collections, copy
# Recursively remove None, from list/tuple elements, and dict key/values.
# NOTE: Changes type of iterable to list, except for strings and tuples.
# NOTE: We don't RECURSE KEYS.
# When "beImmutable=False", may modify "data".
# Result may have different collection types; similar to "filter()".
def StripNones(data, beImmutable=True):
t = type(data)
if issubclass(t, dict):
return _StripNones_FromDict(data, beImmutable)
elif issubclass(t, collections.Iterable):
if issubclass(t, basestring):
# Don't need to search a string for None.
return data
# NOTE: Changes type of iterable to list.
data = [StripNones(x, beImmutable) for x in data if x is not None]
if issubclass(t, tuple):
return tuple(data)
return data
# Modifies dict, removing items whose keys are in keysToRemove.
def RemoveKeys(dict, keysToRemove):
for key in keysToRemove:
dict.pop(key, None)
# Recursively remove None, from dict key/values.
# NOTE: We DON'T RECURSE KEYS.
# When "beImmutable=False", may modify "data".
def _StripNones_FromDict(data, beImmutable):
keysToRemove = []
newItems = []
for item in data.iteritems():
key = item[0]
if None in item:
# Either key or value is None.
keysToRemove.append( key )
else:
# The value might change when stripped.
oldValue = item[1]
newValue = StripNones(oldValue, beImmutable)
if newValue is not oldValue:
newItems.append( (key, newValue) )
somethingChanged = (len(keysToRemove) > 0) or (len(newItems) > 0)
if beImmutable and somethingChanged:
# Avoid modifying the original.
data = copy.copy(data)
if len(keysToRemove) > 0:
# if not beImmutable, MODIFYING ORIGINAL "data".
RemoveKeys(data, keysToRemove)
if len(newItems) > 0:
# if not beImmutable, MODIFYING ORIGINAL "data".
data.update( newItems )
return data
# ---------- TESTING ----------
# When run this file as a script (instead of importing it):
if (__name__ == "__main__"):
from collections import OrderedDict
maxWidth = 100
indentStr = '. '
def NewLineAndIndent(indent):
return '\n' + indentStr*indent
#print NewLineAndIndent(3)
# Returns list of strings.
def HeaderAndItems(value, indent=0):
if isinstance(value, basestring):
L = repr(value)
else:
if isinstance(value, dict):
L = [ repr(key) + ': ' + Repr(value[key], indent+1) for key in value ]
else:
L = [ Repr(x, indent+1) for x in value ]
header = type(value).__name__ + ':'
L.insert(0, header)
#print L
return L
def Repr(value, indent=0):
result = repr(value)
if (len(result) > maxWidth) and \
isinstance(value, collections.Iterable) and \
not isinstance(value, basestring):
L = HeaderAndItems(value, indent)
return NewLineAndIndent(indent + 1).join(L)
return result
#print Repr( [11, [221, 222], {'331':331, '332': {'3331':3331} }, 44] )
def printV(name, value):
print( str(name) + "= " + Repr(value) )
print '\n\n\n'
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 ) ]
printV( 'ORIGINAL data', data )
printV( 'StripNones(data)', StripNones(data) )
print '----- beImmutable = True -----'
#printV( 'data', data )
printV( 'data2', data2 )
#printV( 'data3', data3 )
print '----- beImmutable = False -----'
StripNones(data, False)
#printV( 'data', data )
printV( 'data2', data2 )
#printV( 'data3', data3 )
print
输出:
ORIGINAL data= list:
. [None, 22, (None,), (None, None), None]
. tuple:
. . (None, 202)
. . {32: 302, 33: (501, (None, 999), None, None, 504), None: 301}
. . OrderedDict:
. . . None: 401
. . . 12: 402
. . . 13: None
. . . 14: {'four': 'sixty', 1: 601, 2: None, None: 603}
StripNones(data)= list:
. [22, (), ()]
. tuple:
. . (202,)
. . {32: 302, 33: (501, (999,), 504)}
. . OrderedDict([(12, 402), (14, {'four': 'sixty', 1: 601})])
----- beImmutable = True -----
data2= {'four': 'sixty', 1: 601, 2: None, None: 603}
----- beImmutable = False -----
data2= {'four': 'sixty', 1: 601}
关键点:
if issubclass(t, basestring):
避免在字符串内搜索,因为这没有意义,AFAIK。
if issubclass(t, tuple):
将结果转换回元组。
对于字典,使用copy.copy(data)
来返回与原始字典相同类型的对象。
限制:不会尝试为除以下类型之外的类型保留集合/迭代器类型:list,tuple,dict(及其子类)。
如果需要更改,默认用法会复制数据结构。为False
传递beImmutable
可以在大量数据时获得更高的性能,但会改变原始数据,包括更改嵌套的数据片段 - 这可能会被代码中其他地方的变量引用