0

我有一个格式如下的输入文件。这只是一个示例文件,实际文件有许多相同的条目:

0.0 aa:bb:cc dd:ee:ff 100  000 ---------->line1
0.2 aa:bb:cc dd:ee:ff 101  011 ---------->line2
0.5 dd:ee:ff aa:bb:cc 230  001 ---------->line3
0.9 dd:ee:ff aa:bb:cc 231  110 ---------->line4
1.2 dd:ee:ff aa:bb:cc 232  101 ---------->line5
1.4 aa:bb:cc dd:ee:ff 102  1111 ---------->line6
1.6 aa:bb:cc dd:ee:ff 103  1101 ---------->line7
1.7 aa:bb:cc dd:ee:ff 108  1001 ---------->line8
2.4 dd:ee:ff aa:bb:cc 233  1000 ---------->line9  
2.8 gg:hh:ii jj:kk:ll 450  1110 ---------->line10
3.2 jj:kk:ll gg:hh:ii 600  010 ---------->line11  

第一列代表时间戳,第二个源地址,第三个目标地址,第四个序列号,第五个不需要。

对于这个问题,组的定义:

i. The lines should be consecutive(lines 1 and 2)  
ii. Should have same second and third column, but fourth column should be differed by 1.  

对于对应于相同(column2,column3)的所有组,我需要计算组中第一行和下一行第一行的时间戳差异。
例如,(aa:bb:cc dd:ee:ff) 对应的组是 (line1, line2) & (lin6, line7) & (line8)。最终输出应该是,(aa:bb:cc dd:ee:ff) = [1.4 0.3]。
因为 1.4 = line6,line1 之间的时间戳差异。0.3 是第 8 行,第 6 行 (aa:bb:cc dd:ee:ff) 条目之间的时间差。
这些应该针对所有 (column2 column3) 对进行计算。

我编写了一个程序来计算组中的成员数量,如下所示:

#!/usr/bin/python

with open("luawrite") as f:
#read the first line and set the number from it as the value of `prev`
    num = next(f).rsplit(None,2)[-2:]
    prev  = int(num)
    count = 1                               #initialize `count` to 1
    for lin in f:
            num = lin.rsplit(None,2)[-2:]
            num  = int(num)                    #use `str.rsplit` for minimum splits
            if num - prev == 1:               #if current `num` - `prev` == 1
                    count+=1                          # increment `count`
                    prev = num                        # set `prev` = `num`
            else:
                    print count                #else print `count` or write it to a file
                    count = 1                        #reset `count` to 1
                    prev = num                       #set `prev` = `num`
    if num - prev !=1:
            print count  

我通过将第 2 列和第 3 列作为字典键尝试了各种方法,但是有多个组对应于同一个键。这听起来对我来说是一项艰巨的任务。请帮我解决这个棘手的问题。

4

2 回答 2

2
from collections import defaultdict

data = list()
groups = defaultdict(list)
i = 1
with open('input') as f:
    for line in f:
        row = line.strip().split() + [ i ]
        gname = " ".join(row[1:3])
        groups[gname] += [ row ]
        i += 1

output = defaultdict(list)
for gname, group in groups.items():
    gr = []
    last_key,last_col4, last_idx='',-1,-1
    for row in group:
        key, idx = " ".join(row[1:3]), int(row[-1])
        keys_same   = last_key == key and last_col4 + 1 == int(row[3])
        consequtive = last_idx + 1 == idx
        if not (gr and keys_same and consequtive):
            if gr: output[gr[0][1]] += [ float(row[0]) - float(gr[0][0]) ]
            gr = [ row ]
        else: gr += [ row ]
        last_key, last_col4, last_idx = key, int(row[3]), idx

for k,v in output.items():
    print k, ' --> ', v
于 2013-06-27T00:50:02.667 回答
1

itertools.groupby()可用于提取由以下定义的组:

一世。这些行应该是连续的(第 1 行和第 2 行)

ii. 应该有相同的第二列和第三列,但第四列应该相差 1

然后collections.defaultdict()可用于收集时间戳以查找差异:

对于对应于相同(column2,column3)的所有组,我需要计算组中第一行和下一行第一行的时间戳差异。

from collections import defaultdict
from itertools import groupby

import sys
file = sys.stdin # could be anything that yields lines e.g., a regular file

rows = (line.split() for line in file if line.strip())

# get timestamps map: (source, destination) -> timestamps of 1st lines
timestamps = defaultdict(list) 
for ((source, dest), _), group in groupby(enumerate(rows),
                           key=lambda (i, row): (row[1:3], i - int(row[3]))):
    ts = float(next(group)[1][0]) # a timestamp from the 1st line in a group
    timestamps[source, dest].append(ts)

# find differences
for (source, dest), t in sorted(timestamps.items(), key=lambda (x,y): x):
    diffs = [b - a for a, b in zip(t, t[1:])] # pairwise differences   
    info = ", ".join(map(str, diffs)) if diffs else t # support unique
    print("{source} {dest}: {info}".format(**vars()))

输出

aa:bb:cc dd:ee:ff: 1.4, 0.3
dd:ee:ff aa:bb:cc: 1.9
gg:hh:ii jj:kk:ll: [2.8]
jj:kk:ll gg:hh:ii: [3.2]

[]意味着输入中有一组相应的(源地址,目标地址)对,即没有什么可以构造差异。您可以在时间戳列表中添加一个虚拟0.0时间戳,以统一处理所有情况

于 2013-06-27T03:17:31.917 回答