我正在寻找一个包含源IP,目标IP,时间和数据包长度的列表。如果有任何行包含相同的源IP和目标IP,我需要删除重复的行并显示开始时间,停止时间和总包长度。
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')
我希望将这些行合并起来,它应该像这样:
(['172.217.2.161'], ['10.247.15.39'], '13:25:31:044180', '13:25:31:044371', '46')
答案 0 :(得分:0)
您没有向测试代码中添加一些数据,我也不知道这是否是您的问题,但是您错误地比较了
中的值source[counter] and dest[counter] == source[counter+1] and dest[counter+1]
因为它意味着
(source[counter]) and (dest[counter] == source[counter+1]) and (dest[counter+1])
您必须分别比较每个元素
source[counter] == source[counter+1] and dest[counter] == dest[counter+1]
或者您可以比较元组或列表
(source[counter], dest[counter]) == (source[counter+1], dest[counter+1])
您可以使用zip(source, dest):
代替zip(CombinePacket, CombinePacket[1:])
,并且将同时拥有两个合并的行,可用于创建新行。而且您将获得所有数据作为列表,以便可以将列表与[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)
但是我不知道是否可以有三行具有相同的source, dest
以及这三行的预期结果。
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)