如何计算二维列表或字典列表?

How to count 2D List or List of Dictionaries?

我正在从文本文件、IP 地址和数据包类型中获取信息。我将每次出现的 IP/Type 放入 list/dict?然后我想找到计数,如果 IP/Type 出现 x 次,那么我想将该信息移动到另一个列表。

这是我目前尝试过的方法。

字典列表:

data = [{'ip': '192.168', 'type': 'UDP'}, 
        {'ip': '192.173', 'type': 'TCP'}, 
        {'ip': '192.168', 'type': 'UDP'}]

或二维列表

data = [['192.168', 'UDP'], 
        ['192.173', 'TCP'], 
        ['192.168', 'UDP']] 

我想将 192.168, UDP 移动到另一个包含计数的列表,因为它出现了 x 次以上。我试过 Counter 但我只能让它传递 ip,而不是 ip 和类型。

ipInfo = Counter(k['ip'] for k in data if k.get('ip'))
for info, count in ipInfo.most_common():
    print info, count

这只会打印出 192.168, 2 而不是 192.168, UDP, 2

在我的示例中,我希望能够将 [192.168, UDP, 2]{'ip': '192.168', 'type': 'UDP', 'count':2} 添加到另一个列表。

如果您的数据是第二种形式的二维列表,您可以使用:

In [1]: data = [['192.168', 'UDP'], 
   ...:         ['192.173', 'TCP'], 
   ...:         ['192.168', 'UDP']] 

In [2]: c = Counter(tuple(r) for r in data)

In [3]: for info, count in c.most_common():
   ...:     print info, count

(u'192.168', u'UDP') 2
(u'192.173', u'TCP') 1

这里有一些对列表列表和字典列表都适用的东西:

from collections import Counter, Sequence
from pprint import pprint

def move_info(data):
    """ Copy info from data list into another list with an occurence count added.
        data can be a list of dicts or tuples.
    """
    if isinstance(data[0], dict):
        counter = Counter((dct['ip'], dct['type']) for dct in data1)
        result = [dict(ip=info[0], type=info[1], count=count) 
                            for info, count in sorted(counter.items())]
        return result
    elif isinstance(data[0], Sequence):
        counter = Counter(tuple(elem) for elem in data2)
        result = [list(info) + [count] for info, count in sorted(counter.items())]
        return result
    raise TypeError("Can't move info from a {}".format(type(data)))

# list of dicts
data1 = [{'ip': '192.168', 'type': 'UDP'},
         {'ip': '192.173', 'type': 'TCP'},
         {'ip': '192.168', 'type': 'UDP'}]

# list of lists
data2 = [['192.168', 'UDP'],
         ['192.173', 'TCP'],
         ['192.168', 'UDP']]

another_list = move_info(data1)
pprint(another_list)
print('')
another_list = move_info(data2)
pprint(another_list)

输出:

[{'count': 2, 'ip': '192.168', 'type': 'UDP'},
 {'count': 1, 'ip': '192.173', 'type': 'TCP'}]

[['192.168', 'UDP', 2], ['192.173', 'TCP', 1]]
packetDict = {}

data = [['192.168', 'UDP'], 
        ['192.173', 'TCP'], 
        ['192.168', 'UDP']] 

for packetInfo in data:
  packetInfo = tuple(packetInfo)

  if packetInfo not in packetDict:
    packetDict[packetInfo] = 1
  else:
    packetDict[packetInfo] += 1

for pkt,count in packetDict.items():
  print(pkt,count)

结果

('192.168', 'UDP') 2
('192.173', 'TCP') 1