我有一對單詞列表,並試圖將它們準備爲NetworkX讀取的數據。部分腳本遍歷這些對以將它們映射到id號碼(請參閱下面的代碼)。這段代碼會拋出一個Index out of range
-我需要過去的錯誤。這裏有什麼錯誤?列表索引超出範圍
coocs = [['parttim;work'], ['parttim;work'],['parttim;visit'], ['parttim;site'], ['parttim;uncl'], ['parttim;home'], ['parttim;onlin']]
unique_coocs = list(set([row[0] for row in coocs])) # remove redundance
ids = list(enumerate(unique_coocs)) # creates a list of tuples with unique ids and their names for each word in the network
keys = {name: i for i, name in enumerate(unique_coocs)} # creates a dictionary(hash map) that maps each id to the words
links = [] # creates a blank list
for row in coocs: # maps all of the names in the list to their id number
try:
links.append({keys[row[0]]: keys[row[1]]})
except:
links.append({row[0]: row[1]})
什麼線路上的錯誤? –
This:'links.append({row [0]:row [1]})' – textnet
這將有助於給出整個錯誤信息。 – Joel