Python 无法清除字典列表
对象具有字典列表:Python 无法清除字典列表,python,list,dictionary,reset,Python,List,Dictionary,Reset,对象具有字典列表: self.aggregator = [] self.aggregator.append({'type': 'Log', 'entry': logline1}) self.aggregator.append({'type': 'Log', 'entry': logline2}) print self.aggregator 就目前来看,一切正常: [{'type': 'Log', 'entry': 'Content of logline 1'}, {'type': 'Log',
self.aggregator = []
self.aggregator.append({'type': 'Log', 'entry': logline1})
self.aggregator.append({'type': 'Log', 'entry': logline2})
print self.aggregator
就目前来看,一切正常:
[{'type': 'Log', 'entry': 'Content of logline 1'}, {'type': 'Log', 'entry': 'Content of logline 2'}]
然后,我尝试使用不同的方法重置此列表的内容:
self.aggregator = []
del self.aggregator
现在,当我打印列表时,它是空的:
[]
但当我检查列表的长度时,它仍然保持原来的大小:
>> print len(self.aggregator)
2
为什么这是可能的,我错过了什么吗
==更新
完成下面的演示代码。我找出了错误的原因。这是一行“del self.aggregator”。当我删除这一行时,一切正常。在Windows7上使用Python2.7.10和Python2.7.11(32位)在OSX上测试了这种行为
class Checker(object):
aggregator = []
aggregator_max = 10
def __init__(self):
pass
def finalizeAggregator(self):
string = "\n".join([attribute['string'] for attribute in self.aggregator])
# Check for a match on all the aggregator content
match_result = self.checkString(string,"")
if match_result:
# If match has been found, check the aggregator contents one by one
for element in self.aggregator:
self.checkString(element["string"],
element["module"],
use_aggregator=False)
# Clear aggregator
print self.aggregator
self.aggregator = None
self.aggregator = []
del self.aggregator
print self.aggregator
print len(self.aggregator)
def checkString(self, string, module, use_aggregator=False):
# If aggregator should be used
if use_aggregator:
print "USING aggregator"
# As long as the aggregator is not full
if len(self.aggregator) <= self.aggregator_max:
#if string not in self.aggregator:
# Add element to aggregator
self.aggregator.append({"string": string,
"module": module})
# Process aggregator if full
if len(self.aggregator) >= self.aggregator_max:
self.finalizeAggregator()
# Otherwise return
else:
return False
else:
print "NOT using aggregator"
if "evil" in string:
print "WARNING!!!"
if __name__ == '__main__':
checker = Checker()
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is evil', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
checker.checkString('This is benign', 'test', use_aggregator=True)
类检查器(对象):
聚合器=[]
聚合器_max=10
定义初始化(自):
通过
def finalizeAggregator(自我):
string=“\n”.join([attribute['string']表示self.aggregator中的属性])
#检查所有聚合器内容是否匹配
match_result=self.checkString(字符串“”)
如果匹配结果:
#如果找到匹配项,请逐个检查聚合器内容
对于self.aggregator中的元素:
self.checkString(元素[“string”],
元素[“模块”],
使用(聚合器=False)
#清除聚合器
打印自动聚合器
self.aggregator=None
self.aggregator=[]
del self.aggregator
打印自动聚合器
打印透镜(自聚集器)
def checkString(self、string、module、use_aggregator=False):
#是否应该使用聚合器
如果使用聚合器:
打印“使用聚合器”
#只要聚合器未满
如果len(self.aggregator)=self.aggregator_max:
self.finalizeAggregator()
#否则返回
其他:
返回错误
其他:
打印“不使用聚合器”
如果字符串中有“邪恶”:
打印“警告!!!”
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
checker=checker()
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('This is evil','test',use_aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
checker.checkString('这是良性的','测试',使用\u aggregator=True)
您将聚合器定义为类属性;在您直接分配给self.attribute
之前,它是一个共享类属性。但当你这样做的时候:
self.aggregator = None
自动激活实例属性的。因此,在这一点之后,它是一个完全不同的变量,用于阴影(隐藏)class属性。除非执行del self.aggregator
,否则将删除实例属性,并取消类属性的阴影,以查看原始(共享)值
如果目标是拥有实例属性,请使用实例属性。更改:
class Checker(object):
aggregator = []
aggregator_max = 10
def __init__(self):
pass
致:
它将仅仅是一个实例变量(如果以后打算使用它,请不要使用实际属性,这是毫无意义的;不过,分配空的列表
也可以)
请注意,分配空的
列表
(替换self.aggregator
中的引用)和删除self.aggregator
引用的列表
的内容(del self.aggregator[:]
或Py3.3+,self.aggregator.clear())之间存在差异如果对self.aggregator
引用的列表
可能有多个引用。查看更多详细信息。您将聚合器定义为类属性;在您直接分配给self.attribute
之前,它是一个共享类属性。但当你这样做的时候:
self.aggregator = None
自动激活实例属性的。因此,在这一点之后,它是一个完全不同的变量,用于阴影(隐藏)class属性。除非执行del self.aggregator
,否则将删除实例属性,并取消类属性的阴影,以查看原始(共享)值
如果目标是拥有实例属性,请使用实例属性。更改:
class Checker(object):
aggregator = []
aggregator_max = 10
def __init__(self):
pass
致:
它将仅仅是一个实例v