Search code examples
pythonindex-error

How can I get rid of "IndexError: string index out of range"


I am trying to find neighbor nodes in a graph network. I have imported data in coa_train and now trying to find neighbor nodes.


import matplotlib.pyplot as plt
from math import isclose
from sklearn.decomposition import PCA
import os
import networkx as nx
import numpy as np
import pandas as pd
#from stellargraph import StellarGraph, datasets
#from stellargraph.data import EdgeSplitter
from collections import Counter
import multiprocessing
#from IPython.display import display, HTML
from sklearn.model_selection import train_test_split

%matplotlib inline

def readTrainingData(tr):
    trainingData = []
    with open(tr) as f:
        for line in f:
            a1, a2 = line.strip().split()
            trainingData.append((a1, a2))
    return trainingData
coa_train = readTrainingData("training.txt")

coa_train

[('8193', '16056'),
 ('24578', '21968'),
 ('24578', '18297'),
 ('24578', '16770'),
 ('24578', '17038'),
 ('8195', '2072'),
 ('8195', '20568'),

----------------------
import collections

def getNeighbors(data):
    
    neighbors=collections.defaultdict(set)
    
    for pair in data:
        
        neighbors[pair[0]].add(pair[1])
        
        neighbors[pair[1]].add(pair[0])
        
        

        return neighbors

coa_neighbors= getNeighbors("coa_train")
  

Here, I am getting an error like:

IndexError                                Traceback (most recent call last)
<ipython-input-41-c775c56181f7> in <module>
     13         return neighbors
     14 
---> 15 coa_neighbors= getNeighbors("coa_train")
     16 

<ipython-input-41-c775c56181f7> in getNeighbors(data)
      5     for pair in data:
      6 
----> 7         neighbors[pair[0]].add(pair[1])
      8 
      9         neighbors[pair[1]].add(pair[0])

IndexError: string index out of range

I can't see any reason for this error,as I believe 0 and 1 index in coa_train data are valid.


Solution

  • You pass a string into the function

    getNeighbors("coa_train")
    

    but it should be the variable instead

    getNeighbors(coa_train)
    

    With a string as argument, the loop

    for pair in data:
    

    will give you single characters. And with a single character, you can't do pair[1] any more.