2016-12-10 8 views
0

私は単語のペアのリストを持っており、NetworkXが読むためのデータとしてそれらを準備しようとしています。スクリプトの一部がペアを反復してID番号にマッピングします(下記のコードを参照)。このコードは、私が過去に取得する必要のあるエラーIndex out of rangeを投げます。ここで間違いは何ですか? len(row)ここ常に1に、あなたが使用することはできませんのでリストの索引が範囲外になっています

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

何行が上のエラーです正常に動作しますか? –

+0

これは 'links.append({row [0]:row [1]})' – textnet

+0

です。エラーメッセージ全体を表示するのに役立ちます。 – Joel

答えて

-1

これは

for row in coocs: # maps all of the names in the list to their id number 
    links.append({row[0]: keys[row[0]]}) 

>>> links 
[{'parttim;work': 2}, {'parttim;work': 2}, {'parttim;visit': 3}, {'parttim;site': 4}, {'parttim;uncl': 0}, {'parttim;home': 5}, {'parttim;onlin': 1}] 
0

間違いは、rowで発生row[1]

修正したコードがあるindex number 1

for row in coocs: 
    links.append(row[0]+':'+str(keys[row[0]])) 
print links 

出力:

"parttim; work:2"、 "parttim; visit:3"、 "parttim; site:4"、 "parttim; uncl:0"、 "parttim; home:5"、

[ 'parttim; ONLIN:1']

関連する問題