I'm looking to iterate through a list that has the source IP, destination IP, time and packet length. If there are any lines that contain the same source IP and destination IP I need to remove the duplicated lines and show the start time, stop time and the total packet length.
def combine_data(source, dest, time, length):
CombinePacket = []
CombinePacket = [(source[i], dest[i], time[i], length[i]) for i in range(len(source))]
counter = 0
line = []
for i, j in zip(source, dest):
if(source[counter] and dest[counter] == source[counter+1] and dest[counter+1]):
print(CombinePacket[counter-1], CombinePacket[counter])
counter+=1
return 0
(['172.217.2.161'], ['10.247.15.39'], '13:25:31.044180', '0')
(['172.217.2.161'], ['10.247.15.39'], '13:25:31.044371', '46')
I'm expecting to combine the lines and it should look like this:
(['172.217.2.161'], ['10.247.15.39'], '13:25:31:044180', '13:25:31:044371', '46')
You didn't add some data to test code and I don't know if this is your problem but you incorrectly compare values in
source[counter] and dest[counter] == source[counter+1] and dest[counter+1]
beacuse it means
(source[counter]) and (dest[counter] == source[counter+1]) and (dest[counter+1])
You have to compare every element separatelly
source[counter] == source[counter+1] and dest[counter] == dest[counter+1]
or you can compare tuples or lists
(source[counter], dest[counter]) == (source[counter+1], dest[counter+1])
Instead of zip(source, dest):
you can use zip(CombinePacket, CombinePacket[1:])
and you will have both combined rows which you can use to create new one. And you will get all data as list so you can compare lists with [source, dest]
results = []
for x, y in zip(combine_packet, combine_packet[1:]):
if (x[0:2] == y[0:2]): # `[source, dest]`
data = [x[0], x[1], x[2], y[2], y[3]]
print(data) # (['172.217.2.161'], ['10.247.15.39'], '13:25:31:044180', '13:25:31:044371', '46')
results.append(data)
But I don't know if there can be three rows with the same source, dest
and what result you expect for these three rows.
def combine_data(source, dest, time, length):
combine_packet = list(zip(source, dest, time, length))
results = []
for x, y in zip(combine_packet, combine_packet[1:]):
if (x[0:2] == y[0:2]):
#print(x) # (['172.217.2.161'], ['10.247.15.39'], '13:25:31.044180', '0')
#print(y) # (['172.217.2.161'], ['10.247.15.39'], '13:25:31.044371', '46')
data = [x[0], x[1], x[2], y[2], y[3]]
print(data) # (['172.217.2.161'], ['10.247.15.39'], '13:25:31:044180', '13:25:31:044371', '46')
results.append(data)
return results
source = [['172.217.2.161'], ['172.217.2.161'], ['0.0.0.0']]
dest = [['10.247.15.39'], ['10.247.15.39'], ['10.247.15.39']]
time = ['13:25:31.044180', '13:25:31.044371', '13:25:31.044371']
length = ['0', '46', '123']
combine_data(source, dest, time, length)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.