2017-05-15 2 views
0

Ich habe diese DatenGruppenliste des gleichen Wertes von Position - Python

list_ip = ["192.168.2.9", "192.168.2.8", "192.168.2.7", "192.168.2.6"] 
list_traffic = [ 
    [u'192.168.2.9', u'23.67.224.83', u'10', u'2', u'*', u'*'], 
    [u'23.67.208.186', u'192.168.2.9', u'10', u'1', u'*', u'*'], 
    [u'192.168.2.7', u'74.125.139.124', u'10', u'1', u'*', u'*'], 
    [u'23.67.208.186', u'192.168.2.7', u'10', u'1', u'*', u'*'], 
    [u'23.67.208.186', u'192.168.2.7', u'10', u'1', u'*', u'*'], 
    [u'192.168.2.9', u'74.125.139.125', u'10', u'1', u'*', u'*'], 
    [u'23.67.208.186', u'192.168.2.7', u'10', u'1', u'*', u'*'], 
] 

.

#list_example: [u'192.168.2.9', u'23.67.224.83', u'10', u'2', u'*', u'*'] 
#position 0=src_ip 
#position 1=dst_ip 
#position 2=bytes 
#position 3=packets 

Mein Ziel ist es, alle Werte der Position 2 (Bytes) zu summieren, wobei src_ip = 192.168.2.9, auch der gleiche Prozess für dst_ip = 192.168.2.9.

Src_ip und dst_ip in list_ip sein muss

Erwartetes Ergebnis

# Result 
# new_list_traffic = { 
#  "192.168.2.9": [20, 10], 
#  "192.168.2.7": [10, 30], 
# } 
#192.168.2.9 = 20/10 download/upload bytes 
#192.168.2.7 = 10/30 download/upload bytes 

Ich habe versucht, die folgende Art und Weise, aber das nimmt mich ca. 2 Sekunden, ist es der beste Weg, die Daten zu einer Gruppe?

def trafico_clientes2(request): 
    start_time = time.clock() 
    #list_example: [u'192.168.2.9', u'23.67.224.83', u'10', u'2', u'*', u'*'] 
    #0=src_ip, 1=dst_ip, 2=bytes, 3=packets 

    list_ip = ["192.168.2.9", "192.168.2.8", "192.168.2.7", "192.168.2.6"] 
    list_traffic = [ 
     [u'192.168.2.9', u'23.67.224.83', u'10', u'2', u'*', u'*'], 
     [u'23.67.208.186', u'192.168.2.9', u'10', u'1', u'*', u'*'], 
     [u'192.168.2.7', u'74.125.139.124', u'10', u'1', u'*', u'*'], 
     [u'23.67.208.186', u'192.168.2.7', u'10', u'1', u'*', u'*'], 
     [u'23.67.208.186', u'192.168.2.7', u'10', u'1', u'*', u'*'], 
     [u'192.168.2.9', u'74.125.139.125', u'10', u'1', u'*', u'*'], 
     [u'23.67.208.186', u'192.168.2.7', u'10', u'1', u'*', u'*'], 
    ] 
    new_list_traffic = { } 

    for traffic_ip in list_traffic: 
     src_ip = traffic_ip[0] 
     dst_ip = traffic_ip[1] 
     bytes = int(traffic_ip[2]) 
     if src_ip in list_ip: 
      #bytes download 
      total_bytes = new_list_traffic.get(src_ip) 
      if total_bytes == None: 
       new_list_traffic[src_ip] = [bytes, 0] 
      else: 
       total_bytes [0] = total_bytes [0] + bytes 
       new_list_traffic[src_ip] = total_bytes 

     elif dst_ip in list_ip: 
      #bytes upload 
      total_bytes = new_list_traffic.get(dst_ip) 
      if total_bytes == None: 
       new_list_traffic[dst_ip] = [0, bytes] 
      else: 
       total_bytes [1] = total_bytes [1] + bytes 
       new_list_traffic[dst_ip] = total_bytes 
    # Result 
    # new_list_traffic = { 
    #  "192.168.2.9": [20, 10], 
    #  "192.168.2.7": [10, 30], 
    # } 
    #192.168.2.9 = 20/10 download/upload bytes 
    #192.168.2.7 = 10/30 download/upload bytes 

    total_tiempo = time.clock() - start_time, "seconds" 
    return render(request, 'trafico.html',{"datos": list_traffic, "lista_trafico": new_list_traffic, "total_tiempo": total_tiempo}) 

Vielen Dank für Ihre Hilfe.

Antwort

0
>>> from collections import defaultdict 
>>> results = defaultdict(int) 
>>> resultd = defaultdict(int) 

>>> for data in list_traffic: 
...  results[data[0]] += int(data[2]) 
...  resultd[data[1]] += int(data[2]) 

>>> new_list_traffic = dict() 
>>> for ip in list_ip: 
...  new_list_traffic[ip] = [results[ip], resultd[ip]] 
>>> new_list_traffic 
{ 
    '192.168.2.8': [0, 0], 
    '192.168.2.9': [20, 10], 
    '192.168.2.6': [0, 0], 
    '192.168.2.7': [10, 30] 
} 
+0

hoffe das wird für dich funktionieren – pramod

+0

@oscar immer bereit zu helfen, bitte upvote und akzeptieren als die richtige antwort. – pramod

+0

Wirklich vielen Dank, ich kann nicht nur die Antwort annehmen – oscar

Verwandte Themen