检查一系列字典中的公共密钥

2024-10-08 19:22:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一系列python词典,它们是从控制文件生成的。控制文件只是一个主机名列表,我们用来解析网页中的数据。如果在url中列出了主机名,那么我会将整行数据存储在字典中,基于主机名是键,行是值。在创建了各种字典之后,我从中得到了以下输出。你知道吗

audit_dicts = {
   "us_osdata":us_osdata, 
   "us_weblogic":us_weblogic, 
   "us_tomcat":us_tomcat
   blah
   blah
   }
for key in audit_dicts:
    print "Length of the %s dictionary is %d lines." % (key, len(audit_dicts[key]))

输出:

Total number of hosts in the control file: 4376
----------------------------------------------------------------------------------------------------
The length of the us_mq dictionary is 266 lines.
The length of the us_oracle dictionary is 198 lines.
The length of the uk_mq dictionary is 59 lines.
The length of the us_osdata dictionary is 765 lines.
The length of the us_websphere_ut dictionary is 137 lines.
The length of the uk_websphere dictionary is 33 lines.
The length of the uk_osdata dictionary is 228 lines.
The length of the uk_oracle dictionary is 41 lines.
The length of the us_weblogic dictionary is 144 lines.
The length of the us_jboss dictionary is 59 lines.
The length of the us_sunweb dictionary is 80 lines.
The length of the us_websphere dictionary is 165 lines.
The length of the us_ihs dictionary is 147 lines.
The length of the us_tcserver dictionary is 0 lines.
The length of the uk_weblogic dictionary is 5 lines.
The length of the us_tomcat dictionary is 236 lines.

我想循环检查每个主机名的控制文件,并在一行中打印与该主机名相关联的所有数据,这些数据存储在audit目录的字典中。你知道吗

“伪代码”“”

for x in control:
        combine = {}
        if x in ** any ** of the audit_dicts[key]
            append values aka lines from all dicts  to combined dictionary
        else
            append x as the key and value.

抱歉,这可能是个愚蠢的问题,因为我对编程完全陌生。任何帮助都将不胜感激。你知道吗


Tags: ofthe数据keydictionaryisauditlength
1条回答
网友
1楼 · 发布于 2024-10-08 19:22:50

您可以事先将它们组合起来:

from collections import defaultdict

combined = defaultdict(list)

for d in dicts:
    for key, value in d.items():
        combined[key].append(value)

现在,combined包含每个字典的值列表。你知道吗

相关问题 更多 >

    热门问题