2017-04-22 51 views
2

我從文本文件中獲取信息,IP地址和數據包的類型。我將IP/Type的每次出現置於列表/字典中?然後我想查找計數,如果該IP /類型出現x次,那麼我想將該信息移動到另一個列表。如何統計2D列表或詞典列表?

這是我到目前爲止嘗試過的。

類型的字典列表:

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

或2維列表

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

我想移動192.168,UDP與計數另一個列表,因爲它表現出了比X倍以上。我試過Counter,但我只能通過ip,而不是ip和type。

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

這隻能打印出192.168, 2192.168, UDP, 2

從我的榜樣,我希望能夠添加[192.168, UDP, 2]{'ip': '192.168', 'type': 'UDP', 'count':2}到另一個列表。

+0

你的意思是像計數器(在data.iteritems K [ 'IP']對於k()如果k.get( 'IP'))或計數器(data.iteritems())。這兩個似乎都沒有工作。 –

回答

0

如果你的數據在你的第二個形式的二維列表,你可以使用:

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

這是完美的。非常感謝。我對python還很陌生,所以我不熟悉所有這些數據結構,剛剛瞭解了字典。 –

0

這裏的東西,將兩個清單 - 的 - 列表和列表的-類型的字典工作:

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]] 
0
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) 

RESSULT

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