0

I am trying to take the key and values in input .txt and produce the out put as in output.txt,basically for every value which in-turn is a key and has a value is put in oneline and the lines where the values are present are removed.... I can write the python implementation,I need inputs on how to get started on this..

'''
INPUT(input.txt):-
#KEY    VALUE
353311 
344670 
332807 353314
338169 334478
334478 
353314 353311


OUTPUT(output.txt):-
KEY     VALUE
344670
332807 353314 353311
338169 334478
'''

with open('input.txt', 'rb') as f:
    mainlist = [line.strip() for line in f]
        print mainlist
4

2 回答 2

1

以下是使用NetworkX 库的方法:

import networkx as nx

inp = '''353311 
344670 
332807 353314
338169 334478
334478 
353314 353311'''.splitlines()

G = nx.DiGraph()
for line in inp:
    spl = line.split()
    if len(spl) == 1:
        G.add_node(spl[0])
    else:
        G.add_edge(spl[0], spl[1])

print nx.weakly_connected_components(G)
#[['353314', '332807', '353311'], ['338169', '334478'], ['344670']]
于 2013-06-14T08:52:47.627 回答
1
from collections import OrderedDict
with open('abc') as f:
    dic = OrderedDict()
    seen = set()
    for line in f:
        spl = line.split() + [""]
        key ,v = spl[:2]
        if v in dic and dic[v] == [""]:
            del dic[v]
        for k1,v1 in dic.items():
            if key in v1:
               dic[k1].append(v)
               break
        else:
             dic[key] = [v]

with open('ouput.txt', 'w') as f:
    for k,v  in dic.items():
        f.write("{} {}\n".format(k," ".join(v)))

输出:

344670 
332807 353314 353311
338169 334478 
于 2013-06-13T23:53:50.750 回答