动态嵌套字典 [英] Dynamic nested dictionaries

查看:124
本文介绍了动态嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是为了开始,我知道这里有一些类似的问题,但是没有一个这样解释,也不是问题范围相同。

Just to begin I know there are a couple similarly-titled questions on here, but none are explained quite this way nor is the problem scope the same.

我想要动态添加嵌套字典条目。

I want to add nested dictionary entries dynamically.

因此,用例是:
我有一个监视网络的python脚本。为观察到的每个观察到的IP协议(tcp,udp,icmp)创建一个字典。然后,创建一个子字典,该键是作为每个这些IP协议(80,443等)的目标端口(如果存在)的一个密钥(注意,它是否将服务器端口视为源或目的地,但通常服务器是目的地,所以我选择HTTP和HTTPS作为示例)。对于这些目的地端口中的每一个,创建与服务器IP相对应的密钥(例如,www.google.com的IP)。然后,另一个具有时间戳的字典首先被视为密钥,密钥的数据/值是客户端的IP。

The use case is thus: I have a python script that is monitoring the network. A dictionary is created for each observed IP protocol (tcp, udp, icmp) that is observed. Then a sub-dictionary is created with a key that is the destination port (if one exists) for each of these IP protocols (80, 443, etc.) (note it doesn't matter whether it sees the server port as the source or destination, but normally servers are the destination so I picked HTTP and HTTPS as examples). For each of these destination ports a key is created corresponding to the server IP (e.g., the IP for www.google.com). And then another dictionary with the timestamp the session was first observed as the key and the data/value for the key being the client's IP.

但是,我需要这个随着时间的推移,随着时间的流逝,我不会在执行之前也没有数据,也没有初始化数据。

However, I want need this to be populated as time goes on since I won't have the data before execution nor at initialization.

输出将类似于:

{  'icmp' :
   {  'echo-request' :
       {  '<ip_of_www.google.com>' :
           {   '<timestamp>' : <some_client_ip> }
       }
      'echo-reply' :
       {  '<ip_of_www.google.com>' :
           {   '<timestamp>' : <some_client_ip> }
       }
   }
   'tcp' :
   {
      '80' 
       {  '<ip_of_www.google.com>' :
           {   '<timestamp>' : <some_client_ip> }
           {   '<timestamp>' : <some_client_ip> }
       }
      '443' 
       {  '<ip_of_encrypted.google.com>' :
           {   '<timestamp>' : <some_client_ip> }
           {   '<timestamp>' : <some_client_ip> }
           {   '<timestamp>' : <some_client_ip> }
           {   '<timestamp>' : <some_client_ip> }
       }
   }
}

谢谢! >

Thanks!

推荐答案

你是:

def set_nested(dict, value, *path):
    for level in path[:-1]:
        dict = dict.setdefault(level, {})

    dict[path[-1]] = value


d = {}

set_nested(d, '127.0.0.1', 'icmp', 'echo', 'google.com', '1 dec 2014')
set_nested(d, '127.0.0.1', 'icmp', 'echo', 'google.com', '2 dec 2014')
set_nested(d, '127.0.0.1', 'icmp', 'echo', 'yahoo.com', '2 dec 2014')
set_nested(d, 'error', 'udp')

from pprint import pprint
pprint(d)

输出:

{'icmp': {'echo': {'google.com': {'1 dec 2014': '127.0.0.1',
                                  '2 dec 2014': '127.0.0.1'},
                   'yahoo.com': {'2 dec 2014': '127.0.0.1'}}},
 'udp': 'error'}

我还建议您查看 json tinydb 如果你想存储和查询结果。

I'd also suggest you to have a look at json and tinydb if you want to store and query results.

这篇关于动态嵌套字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆