2014-02-27 42 views
2

什麼是最有效的方式來轉換此文件:轉換被座標表示以adjecency列表表示

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等的解決方案。

回答

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

在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) 

這假定輸入和輸出文件是不同的:你可以只寫輸出到標準輸出和離開它作爲用戶的問題來保存它。

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) 
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 
+0

+1,awk的岩石!這種排序需要一個'-n',不過,對吧? – elias

+0

'-n'永遠不會傷害,因爲我們正在比較數字 –