在字典中标识重复值并在选项卡中打印

2024-05-23 17:03:51 发布

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

我有一个字典(d),其中每个键都可以有多个值(作为列表追加)。你知道吗

例如,字典有以下两个键、值对,其中一个有重复的值,而另一个没有:

SPECIFIC-THREATS , ['5 SPECIFIC-THREATS Microsoft Windows print spooler little endian DoS attempt', '4 SPECIFIC-THREATS obfuscated RealPlayer Ierpplug.dll ActiveX exploit attempt', '4 SPECIFIC-THREATS obfuscated RealPlayer Ierpplug.dll ActiveX exploit attempt']

以及

TELNET , ['1 TELNET bsd exploit client finishing']

我想浏览整个字典,检查是否有任何键有重复的值,然后将结果打印在一个表中,该表中有键、重复值的数量、值(多次出现)等列。你知道吗

到目前为止,我掌握的情况如下:

import texttable
import collections

def dupechecker():

 t = texttable.Texttable()
 for key, value in d.iteritems():
   for x, y in collections.Counter(value).items():
     if y > 1:
      t.add_rows([["Category", "Number of dupe values", "Value which appears multiple times"], [key, y, x]])
      print t.draw()

它可以工作,但是没有任何重复值的键(在本例中是TELNET)不会出现在表输出中(因为该表是在if条件语句中打印的)。这就是我得到的:

+-------------------------+-------------------------+-------------------------+
|        Category         |  Number of dupe values  |   Value which appears   |
|                         |                         |     multiple times      |
+=========================+=========================+=========================+
| SPECIFIC-THREATS        | 2                       | 4 SPECIFIC-THREATS      |
|                         |                         | obfuscated RealPlayer   |
|                         |                         | Ierpplug.dll ActiveX    |
|                         |                         | exploit attempt         |
+-------------------------+-------------------------+-------------------------+

不管怎样,我都可以跟踪每个键的有趣参数(重复值和多次出现的值的数量),然后一起打印。我希望输出像:

+-------------------------+-------------------------+-------------------------+
|        Category         |  Number of dupe values  |   Value which appears   |
|                         |                         |     multiple times      |
+=========================+=========================+=========================+
| SPECIFIC-THREATS        | 2                       | 4 SPECIFIC-THREATS      |
|                         |                         | obfuscated RealPlayer   |
|                         |                         | Ierpplug.dll ActiveX    |
|                         |                         | exploit attempt         |
+-------------------------+-------------------------+-------------------------+
| TELNET                  | 0                       |                         |
|                         |                         |                         |
|                         |                         |                         |
|                         |                         |                         |
+-------------------------+-------------------------+-------------------------+

更新

已解决


Tags: ofnumber字典telnetdllcategoryspecificexploit
1条回答
网友
1楼 · 发布于 2024-05-23 17:03:51

只需将dupechecker更改为也为“non-duplicates”添加行,但每个类别只添加一次,在循环之前添加标题,完成后打印表。你知道吗

def dupechecker():

 t = texttable.Texttable()
 t.header(["Category", "Number of dupe values", "Value which appears multiple times"])
 for key, value in d.iteritems():
   has_dupe = False
   for x, y in collections.Counter(value).items():
     if y > 1:
      has_dupe = True
      t.add_row([key, y, x])
   if not has_dupe:
      t.add_row([key, 0, ''])
 print t.draw()

相关问题 更多 >