将坐标表示转换为邻接列表表示

时间:2014-02-27 12:30:41

标签: python graph awk

转换此文件的最有效方法是什么:

10 3
10 5
12 6
12 19
19 12 
19 14 
19 10

到此:

10 3 5
12 6 19
19 12 14 10

输入文件的第一列按递增顺序进行数字排序。

欢迎任何使用Python,AWK等的解决方案。

4 个答案:

答案 0 :(得分:2)

from itertools import groupby
lines, op_file = [line.split() for line in open("In.txt")], open("Out.txt", "w")
for key, grp in groupby(lines, key = lambda x: x[0]):
    print >> op_file, "{} {}".format(key, " ".join([i[1] for i in grp]))
op_file.close()

<强>输出

10 3 5
12 6 19
19 12 14 10

答案 1 :(得分:2)

既然你提到了awk:

$ awk '{a[$1]=a[$1]" "$2}END{for (i in a){print i a[i]}}' input
19 12 14 10
10 3 5
12 6 19

将它管道传输到sort以获得它,好吧,排序:

$ awk '...' input | sort
10 3 5
12 6 19
19 12 14 10

答案 2 :(得分:1)

在Python 2中:

import itertools, operator

with open(infilename) as infile:
    input = (line.split() for line in infile)
    output = itertools.groupby(input, operator.itemgetter(0))
    with open(outfilename, 'w') as outfile:
        for key, line in output:
            print >>outfile, key, ' '.join(val[1] for val in line)

这假设输入和输出文件不同:您可以将输出写入标准输出并将其保留为用户的问题以保存它。

答案 3 :(得分:0)

试试这段代码

fp = open('/tmp/test.txt')

list_dict = {}

for line in fp.readlines():
    split_values = line.split()
    if split_values[0] in list_dict:
        list_dict[split_values[0]].extend(split_values[1:])
    else:
        list_dict[split_values[0]] = split_values

for val in  list_dict.values():
    print " ".join(val)