识别字典中的重复值并在表格中打印

时间:2014-12-30 16:31:15

标签: python dictionary

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

例如,字典具有以下两个键值对,其中一个具有重复值,而其他值不具有:

  

特定威胁,[' 5具体威胁Microsoft Windows打印   spooler little endian DoS尝试' 4' 4 SPECIFIC-THREATS混淆   RealPlayer Ierpplug.dll ActiveX漏洞利用尝试',' 4特定威胁   模糊的RealPlayer Ierpplug.dll ActiveX漏洞利用尝试']

  

TELNET,[' 1 TELNET bsd利用客户端整理']

我想查看整个字典,检查是否有任何键具有重复值,然后将结果打印在具有键,重复值数,值(显示多次)等的表中作为列。

这是我到目前为止所做的:

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                       |                         |
|                         |                         |                         |
|                         |                         |                         |
|                         |                         |                         |
+-------------------------+-------------------------+-------------------------+

更新

解决

1 个答案:

答案 0 :(得分:1)

只需将dupechecker更改为"非重复",但每个类别只添加一次,在循环之前添加标题,并在完成后打印表格。

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()