查找文本文件中的所有链接ID,每行包含两个链接的ID

时间:2015-11-11 16:20:35

标签: python

我有一个包含以下行的文本文件:
ID1 ID2
ID1 ID3
ID1 ID4
ID2 ID1
ID2 ID3
ID2 ID4
ID3 ID1
ID3 ID2
ID3 ID4
ID4 ID1
ID4 ID2
ID4 ID3
ID5 ID6
ID5 ID7
ID6 ID5
ID6 ID7
ID7 ID5
ID7 ID6
.....

我想查找所有链接的ID:
[ID1,ID2,ID3,ID4],[ID5,ID6,ID7]

我在python中有点新鲜。你能否分享一下如何解决这个问题?

非常感谢!

1 个答案:

答案 0 :(得分:0)

好吧这不是最好的python但是因为你是新手,它会向你展示一些你将来可以使用的基本概念:

ld = {} # basic dictionary
# now open & read file
with open('linked_id.txt') as fin:
    for ln in fin: # process each line in the file
        k, v = ln.split() # split each line eg. k = 'ID1', v = 'ID2'
        # make a dictionary entry - setdefault puts the k(ey) in and
        # in this case set the value to a list, append then adds to 
        # the list by k(ey)
        ld.setdefault(k, []).append(v) 

unq = [] # start an empty list
for k, v in ld.items(): # process the dictionary elements
    v.append(k)  # make the value list include the key
    ll = sorted(v)  # sort the new list
    if not ll in unq: # see if its in the unq (unique) list
        unq.append(ll) # if not add it

print(unq)  # show the unique sets 

现在有各种各样的库函数可供使用,但这将完成工作