確定這是不是最好的蟒蛇,但因爲你是新的它會告訴你,你可以在未來使用的一些基本概念:
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
現在有各種各樣的庫函數可以使用,但這將完成工作
你到目前爲止嘗試過什麼,你是如何從文本文件到該列表? – SirParselot
我對使用unix命令行更有信心。所以,我通過剪切和排序uniq等獲得了ID ... – crazyhottommy
因此,您只需要第二列中的唯一ID? – SirParselot