我创建了以下dictaccs
:
{
'192.168.20.222': [('access_times', 10), ('bytes', 13147), ('errors', 20)],
'192.168.60.200': [('access_times', 15), ('bytes', 118922), ('errors', 10)],
'192.168.60.150': [('access_times', 20), ('bytes', 117989), ('errors', 30)],
'192.168.60.100': [('access_times', 40), ('bytes', 134922), ('errors', 15)],
...
}
如何访问它以检索具有最大访问次数计数的IP的报告?/最大字节数等?你知道吗
下面是生成dict的代码:
accs = defaultdict(list)
for i in reqs_host.keys():
if reqs_host[i] > max_requests:
accs[i].append(('access_times', reqs_host[i]))
if bytes_host[i] > max_bytes:
accs[i].append(('bytes', bytes_host[i]))
if errors_host[i] > max_errors:
accs[i].append(('errors', errors_host[i]))
#
print dict(accs)
如果一些if没有被执行(即errors_host > max_errors
,但是reqs_hosts < max_reqests
),我怎么能在相应的dict中仍然有('access_times', X)
?所以,要有违约金。你知道吗
您可以这样做:
演示:http://repl.it/Rcp
如果每个条目都有相同的属性,我建议使用命名元组。这样,您可以按名称访问值,但不必将名称与每个值一起存储。 回答您的问题:您必须自己将默认值放入列表中。你知道吗
相关问题 更多 >
编程相关推荐