IndexError: Python list index out of range - list

I have an empty list, (r) and declared first element as r[0] = a
import time, urllib.request,random
def getDictionary():
word_site = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
response = urllib.request.urlopen(word_site)
txt = response.read()
return txt.splitlines()
def getWordsList(listOfWords, sample):
word = ""
randWords = []
for i in range(0,sample):
while(len(word) <=2):
word = random.choice(listOfWords).decode('utf-8')
randWords.append(word)
word = ""
return randWords
start = True
noOfWords = 25
words = getDictionary()
wordsList = getWordsList(words, noOfWords)
start = True
print ("\nINSTRUCTIONS\nWhen the coundown gets to zero, type the word in lowercase letters!\n That's the only rule!")
name = input("What is your name? ")
name = name.split(" ")
input("Press enter when ready...")
while start == True:
print("Game will start in: ")
print ("3 seconds")
time.sleep(1)
print ("2 seconds")
time.sleep(1)
print ("1 seconds")
time.sleep(1)
times = []
k = list()
r = list()
for i in range(25):
startTime = time.time()
userWord = input(str(i+1) + ". " + wordsList[i].capitalize() + " " )
k.append(wordsList[i].capitalize())
if (userWord.lower() == wordsList[i].lower()):
endTime = time.time()
times.append(endTime - startTime)
r[i] = str(endTime - startTime)
else:
times.append("Wrong Word")
r[i] = ("Wrong Word")
Above is where I am having a problem.
for i in range(25):
startTime = time.time()
print (str(i+1) + ". " + str(k[i]) + ": " + str(times[i]) )
a = 0
for i in range(25):
a = a+i
for i in range(25):
if r[i] == "Wrong Word":
r = r.pop(i)
b = (a/len(r))
c = round(b, 2)
print (c)
start = False
here is my error:
r[i] = "Wrong Word"
IndexError: list assignment index out of range

The pop() method removes an element from the list and returnes it (see an example). What I think is happening is that at some point the condition of the if statment resolves to true. Next, after calling r.pop(i) r is replaced by its i-th element. It's probpably a string so calling its (i+1)-th element later can result in Index out of range error.
In other words, something like this is happening:
r = ["a", "foo", "bar", "baz"]
for i in range(4):
if r[i] == "a": # for i=0 this gives "a" == "a"
r = r.pop(i) # later,this results in r = "a"
next loop iteration with i = 1 will result in "a"[1] which will result in Index out of range.
All in all instead of:
for i in range(25):
if r[i] == "Wrong Word":
r = r.pop(i)
you could just write:
r = [item for item in r if item != "Wrong word"]
which would be also more pythonic solution.

Related

Getting the connected components in networkx in the order of which edges are added

Each sentence in doc2 is displayed as a graph. Now the edges were added in the form s-o-v from the respective subject_list, object_list and verb_list.
I have tried to display the connected components. But the order in which it displays the sentence is not in the order in which the edges were added.
# This Python file uses the following encoding: utf-8
%matplotlib notebook
import codecs
import itertools
import re
import networkx as nx
import matplotlib.pyplot as pl
from matplotlib.font_manager import FontProperties
prop = FontProperties()
graph = nx.Graph()
labels = {}
each_one = []
list_of_sentences = []
subject_list = []
object_list = []
verb_list = []
newDict = {}
with codecs.open('doc2.txt', encoding='utf-8') as f:
text = f.read()
sentences = re.split(r' *[\.\?!][\'"\)\]]* *', text)
for stuff in sentences:
list_of_sentences.append(stuff)
new_list_of_sentences = []
for d in list_of_sentences:
s = d.replace(u'वतीन', '').replace(u'आनी', '').replace(u'हिणें', '').replace(',', '')
new_list_of_sentences.append(s)
f = open('doc2_tag.txt', 'r')
for line in f:
k, v = line.strip().split('/')
newDict[k.strip().decode('utf-8')] = v.strip()
f.close()
for sentence in new_list_of_sentences:
a = b = c = ""
sentence_word_list = sentence.split()
for word in sentence_word_list:
if newDict[word] == 'N-NNP':
a += word + " "
if newDict[word] == 'N-NN':
b += word + " "
if newDict[word] == 'JJ':
b += word + " "
if newDict[word] == 'QT-QTC':
b += word + " "
if newDict[word] == 'RB':
b += word + " "
if newDict[word] == 'N-NST':
b += word + " "
if newDict[word] == 'PR-PRP':
b += word + " "
if newDict[word] == 'PSP':
b += word + " "
if newDict[word] == 'CC-CCD':
b += word + " "
if newDict[word] == 'V-VM-VF':
c += word + " "
subject_list.append(a)
object_list.append(b)
verb_list.append(c)
konkani_dict = {u'सनरायझर्साक': u'सनरायझर्स', u'सनरायझर्सान': u'सनरायझर्स', u'सनरायझर्साच्या': u'सनरायझर्स'}
for idx, sub in enumerate(subject_list):
temp_list = sub.split(" ")
for i in temp_list:
if i in konkani_dict:
new_sub = sub.replace(i, konkani_dict[i])
subject_list[idx] = new_sub
for s in subject_list:
if s is not "":
graph.add_node(s)
labels[s] = s
for o in object_list:
if o is not "":
graph.add_node(o)
labels[b] = b
for v in verb_list:
if v is not "":
graph.add_node(v)
labels[v] = v
for (s, o, v) in zip(subject_list, object_list, verb_list):
if s and o is not "":
graph.add_edge(s, o)
if o and v is not "":
graph.add_edge(o, v)
pos=nx.spring_layout(graph,k=0.15,iterations=20)
nx.draw(graph, with_labels = True, font_family = "Nirmala UI", node_size = 40, font_size = 9 ,node_color = "darkblue")
pl.show()
sentences=[]
for component in nx.connected_components(graph):
g=(
filter(
lambda x: x[0] in component and x[1] in component,
graph.edges
)
)
p=[]
p= ''.join(item for tuple_ in g for item in tuple_)
print p
sentences.append(p)
print sentences
output=[]
for i in sentences:
inputWords = i.split(" ")
inputWords=inputWords[-1::-1]
output = ' '.join(inputWords)
print output
Expected output is spmething like this:
शिखर धवनान सगळ्यांत चड ४५ धांवड्यो केल्यो ,
सनरायझर्स दीपर हुडा जैतांत पर्जळ्ळो
This is the output I get: sentences displayed
networkx doesn't store the order of created nodes/edges because this information is mostly useless. If you want to have this information, you should add it manually. In your program, for example (for edges):
edge_index = 0
for (s, o, v) in zip(subject_list, object_list, verb_list):
if s and o is not "":
graph.add_edge(s, o, index=edge_index)
edge_index += 1
if o and v is not "":
graph.add_edge(o, v, index=edge_index)
edge_index += 1
Then you should print sorted edges:
sorted( # Sorted list of edges
list(g.edges.data('index')), # With 'index' data
key=lambda x: x[2] # Sorted by 'index' data
)```

GLPK output formats

I am new to GLPK, so my apologies in advance if I'm missing something simple!
I have a largeish LP that I am feeding through GLPK to model an energy market. I'm running the following command line to GLPK to process this:
winglpk-4.65\glpk-4.65\w64\glpsol --lp problem.lp --data ExampleDataFile.dat --output results2.txt
When I open the resulting text file I can see the outputs, which all look sensible. I have one big problem: each record is split over two rows, making it very difficult to clean the file. See an extract below:
No. Row name St Activity Lower bound Upper bound Marginal
------ ------------ -- ------------- ------------- ------------- -------------
1 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1990)_
NS 0 0 = < eps
2 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1991)_
NS 0 0 = < eps
3 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1992)_
NS 0 0 = < eps
4 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1993)_
NS 0 0 = < eps
5 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1994)_
NS 0 0 = < eps
6 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1995)_
NS 0 0 = < eps
7 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1996)_
NS 0 0 = < eps
8 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1997)_
NS 0 0 = < eps
9 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1998)_
NS 0 0 = < eps
10 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1999)_
NS 0 0 = < eps
11 c_e_SpecifiedDemand(UTOPIA_CSV_ID_2000)_
NS 0 0 = < eps
12 c_e_SpecifiedDemand(UTOPIA_CSV_ID_2001)_
NS 0 0 = < eps
13 c_e_SpecifiedDemand(UTOPIA_CSV_ID_2002)_
NS 0 0 = < eps
14 c_e_SpecifiedDemand(UTOPIA_CSV_ID_2003)_
NS 0 0 = < eps
15 c_e_SpecifiedDemand(UTOPIA_CSV_ID_2004)_
NS 0 0 = < eps
I would be very grateful of any suggestions for either:
How I can get each record in the output text file onto a single row, or
Ideas on how to clean / post-process the existing text file output.
I'm sure I'm missing something simple here, but the output is in a very unhelpful format at the moment!
Thanks!
I wrote a Python parser for the GLPK output file. It is not beautiful and not save (try-catch) but it is working (for pure simplex problems).
You can call it on output file:
outp = GLPKOutput('myoutputfile')
print(outp)
val1 = outp.getCol('mycolvar','Activity')
val2 = outp.getRow('myrowname','Upper_bound') # row names should be defined
The class is as follows:
class GLPKOutput:
def __init__(self,filename):
self.rows = {}
self.columns = {}
self.nRows = 0
self.nCols = 0
self.nNonZeros = 0
self.Status = ""
self.Objective = ""
self.rowHeaders = []
self.rowIdx = {}
self.rowWidth = []
self.Rows = []
self.hRows = {}
self.colHeaders = []
self.colIdx = {}
self.colWidth = []
self.Cols = []
self.hCols = {}
self.wcols = ['Activity','Lower_bound','Upper bound','Marginal']
self.readFile(filename)
# split columns with weird line break
def smartSplit(self,line,type,job):
ret = []
line = line.rstrip()
if type == 'ROWS':
cols = len(self.rowHeaders)
idx = self.rowWidth
else:
cols = len(self.colHeaders)
idx = self.colWidth
if job == 'full':
start = 0
for i in range(cols):
stop = start+idx[i]+1
ret.append(line[start:stop].strip())
start = stop
elif job == 'part1':
entries = line.split()
ret = entries[0:2]
elif job == 'part2':
start = 0
for i in range(cols):
stop = start+idx[i]+1
ret.append(line[start:stop].strip())
start = stop
ret = ret[2:]
# print()
# print("SMART:",job,line.strip())
# print(" TO:",ret)
return ret
def readFile(self,filename):
fp = open(filename,"r")
lines = fp.readlines()
fp.close
i = 0
pos = "HEAD"
while pos == 'HEAD' and i<len(lines):
entries = lines[i].split()
if len(entries)>0:
if entries[0] == 'Rows:':
self.nRows = int(entries[1])
elif entries[0] == 'Columns:':
self.nCols = int(entries[1])
elif entries[0] == 'Non-zeros:':
self.nNonZeros = int(entries[1])
elif entries[0] == 'Status:':
self.Status = entries[1]
elif entries[0] == 'Objective:':
self.Objective = float(entries[3]) #' '.join(entries[1:])
elif re.search('Row name',lines[i]):
lines[i] = lines[i].replace('Row name','Row_name')
lines[i] = lines[i].replace('Lower bound','Lower_bound')
lines[i] = lines[i].replace('Upper bound','Upper_bound')
entries = lines[i].split()
pos = 'ROWS'
self.rowHeaders = entries
else:
pass
i+= 1
# formatting of row width
self.rowWidth = lines[i].split()
for k in range(len(self.rowWidth)): self.rowWidth[k] = len(self.rowWidth[k])
# print("Row Widths:",self.rowWidth)
i+= 1
READY = False
FOUND = False
while pos == 'ROWS' and i<len(lines):
if re.match('^\s*[0-9]+',lines[i]): # new line
if len(lines[i].split())>2: # no linebrak
entries = self.smartSplit(lines[i],pos,'full')
READY = True
else: # line break
entries = self.smartSplit(lines[i],pos,'part1')
READY = False
FOUND = True
else:
if FOUND and not READY: # second part of line
entries += self.smartSplit(lines[i],pos,'part2')
READY = True
FOUND = False
if READY:
READY = False
FOUND = False
# print("ROW:",entries)
if re.match('[0-9]+',entries[0]): # valid line with solution data
self.Rows.append(entries)
self.hRows[entries[1]] = len(self.Rows)-1
else:
print("wrong line format ...")
print(entries)
sys.exit()
elif re.search('Column name',lines[i]):
lines[i] = lines[i].replace('Column name','Column_name')
lines[i] = lines[i].replace('Lower bound','Lower_bound')
lines[i] = lines[i].replace('Upper bound','Upper_bound')
entries = lines[i].split()
pos = 'COLS'
self.colHeaders = entries
else:
pass #print("NOTHING: ",lines[i])
i+= 1
# formatting of row width
self.colWidth = lines[i].split()
for k in range(len(self.colWidth)): self.colWidth[k] = len(self.colWidth[k])
# print("Col Widths:",self.colWidth)
i+= 1
READY = False
FOUND = False
while pos == 'COLS' and i<len(lines):
if re.match('^\s*[0-9]+',lines[i]): # new line
if len(lines[i].split())>2: # no linebreak
entries = self.smartSplit(lines[i],pos,'full')
READY = True
else: # linebreak
entries = self.smartSplit(lines[i],pos,'part1')
READY = False
FOUND = True
else:
if FOUND and not READY: # second part of line
entries += self.smartSplit(lines[i],pos,'part2')
READY = True
FOUND = False
if READY:
READY = False
FOUND = False
# print("COL:",entries)
if re.match('[0-9]+',entries[0]): # valid line with solution data
self.Cols.append(entries)
self.hCols[entries[1]] = len(self.Cols)-1
else:
print("wrong line format ...")
print(entries)
sys.exit()
elif re.search('Karush-Kuhn-Tucker',lines[i]):
pos = 'TAIL'
else:
pass #print("NOTHING: ",lines[i])
i+= 1
for i,e in enumerate(self.rowHeaders): self.rowIdx[e] = i
for i,e in enumerate(self.colHeaders): self.colIdx[e] = i
def getRow(self,name,attr):
if name in self.hRows:
if attr in self.rowIdx:
try:
val = float(self.Rows[self.hRows[name]][self.rowIdx[attr]])
except:
val = self.Rows[self.hRows[name]][self.rowIdx[attr]]
return val
else:
return -1
def getCol(self,name,attr):
if name in self.hCols:
if attr in self.colIdx:
try:
val = float(self.Cols[self.hCols[name]][self.colIdx[attr]])
except:
val = self.Cols[self.hCols[name]][self.colIdx[attr]]
return val
else:
print("key error:",name,"not known ...")
return -1
def __str__(self):
retString = '\n'+"="*80+'\nSOLUTION\n'
retString += "nRows: "+str(self.nRows)+'/'+str(len(self.Rows))+'\n'
retString += "nCols: "+str(self.nCols)+'/'+str(len(self.Cols))+'\n'
retString += "nNonZeros: "+str(self.nNonZeros)+'\n'
retString += "Status: "+str(self.Status)+'\n'
retString += "Objective: "+str(self.Objective)+'\n\n'
retString += ' '.join(self.rowHeaders)+'\n'
for r in self.Rows: retString += ' # '.join(r)+' #\n'
retString += '\n'
retString += ' '.join(self.colHeaders)+'\n'
for c in self.Cols: retString += ' # '.join(r)+' #\n'
return retString

Merge code of same operation with two different conditions

I have a question. I'm trying to tidy up some coding.
I have this part of code
if (self.type == self.BETAECM):
for line in contentInfo:
if line.startswith("caid:"):
caid = self.readEcmInfo(line)
if "x" in caid:
idx = caid.index("x")
caid = caid[(idx + 1):]
caid = caid[:4]
caid = caid.upper()
if (caid >= "1700") and (caid <= "17FF"):
return True
elif line.startswith("====="):
caid = self.readCaid(line)
if "x" in caid:
idx = caid.index("x")
caid = caid[(idx + 1):]
caid = caid[:4]
caid = caid.upper()
if (caid >= "1700") and (caid <= "17FF"):
return True
return False
As you can see there are two different conditions that are handled with the same operation
I want to tidy up everything so that the identical parts of the code (what starts with if "x" in caid) are only written once. Is this possible?
Thanks in advance!
I don't know this language, but I'll take a stab at it.
for line in contentInfo:
caid = "nothing"
if line.startswith("caid:"):
caid = self.readEcmInfo(line)
elif line.startswith("====="):
caid = self.readCaid(line)
if "x" in caid:
idx = caid.index("x")
caid = caid[(idx + 1):]
caid = caid[:4]
caid = caid.upper()
if (caid >= "1700") and (caid <= "17FF"):
return True
You need to join them using an OR operator. In other words you what one condition OR the other to be true in order to excecute your code.
if line.startswith("caid:") || line.startswith("====="):
Thank you soooo much!
What I was missing is that the last if statement (if "x" in caid:) had to go on the same level the previous one. It works now!
So if I want to merge another example:
def readEcmInfo(self, line):
if ":" in line:
idx = line.index(":")
line = line[(idx + 1):]
line = line.replace("\n", "")
while line.startswith(" "):
line = line[1:]
while line.endswith(" "):
line = line[:-1]
return line
else:
return ""
elif "CaID" in line:
idx = line.index("D")
line = line[(idx + 1):]
line = line.replace("\n", "")
while line.startswith(" "):
line = line[1:]
while line.endswith(" "):
line = line[:-1]
return line
else:
return ""
Should it go like this?
def readEcmInfo(self, line):
if ":" in line:
idx = line.index(":")
line = line[(idx + 1):]
line = line.replace("\n", "")
elif "CaID" in line:
idx = line.index("D")
line = line[(idx + 1):]
line = line.replace("\n", "")
while line.startswith(" "):
line = line[1:]
while line.endswith(" "):
line = line[:-1]
return line
else:
return ""

List value assignment within for loop

What can I do when I get the following error in this code?
def reverse_word(word):
index = len(word)
new_word = []
for i in range(index - 1, -1, -1):
new_word.append(word[i])
return ''.join(new_word)
def reverse_sentence(sentence):
l = sentence.split()
for i in l:
l[i] = reverse_word(i)
l = ' '.join(l)
print(l)
a = "Hello !Nhoj Want to have lunch?"
reverse_sentence(a)
TypeError: list indices must be integers or slices, not str.
What can I write instead of this line:
l[i] = reverse_word(i)
... this line: l[i] = reverse_word(i) throws an error because i is a string (word), but in l[i], i must be an index.
You probably wanted to do something like this:
words = sentence.plit()
new_sentence = []
for i,word in enumerate(words): #or just for word in words, you don't need the index
new_sentence.append(reverse_word(word))
and then join at return time return ' '.join(new_sentence)
This implementation follows your logic but uses strings instead of lists.
def reverse_word(word):
new_word = ''
for i in range(len(word) - 1, -1, -1):
new_word += word[i]
return new_word
def reverse_sentence(sentence):
r = ''
for word in sentence.split():
r += reverse_word(word) + ' '
return r[:-1]
>>> a = "Hello !Nhoj Want to have lunch?"
>>> reverse_sentence(a)
>>> 'olleH johN! tnaW ot evah ?hcnul'

raw_input() invalid syntax

I want to input a string of five numbers with spaces between them and use raw_input() for it. However the second item(the portion that's between the first and second spaces) is claimed to be a syntax error. Code below:
#class for final output - used as an ad hoc static string
class StatString:
outstring = ""
#function to check if Boom or Trach or both
def BoomTrach(num,B,T):
Boom = False
Trach = False
temp = num
while temp != 0:
if num % B == 0:
Boom == True
break
if (temp % 10) % B == 0:
Boom = True
break
temp = (temp - temp % 10) / 10
temp = num
while temp != 0:
if num % T == 0:
Trach = True
break
if (temp % 10) % T == 0:
Trach = True
break
temp = (temp - temp % 10) / 10
if Boom and Trach:
herestring.outstring = herestring.outstring + "Boom-Trach"
elif Boom:
herestring.outstring = herestring.outstring + "Boom"
elif Trach:
herestring.outstring = herestring.outstring + "Trach"
else:
herestring.outstring = herestring.outstring + str(num)
#start of "main" portion
def main():
inS = raw_input() <<<--- Input here
arr = inS.split(' ')
X = int(arr[0])
Y = int(arr[1])
CountFrom = int(arr[2])
jump = int(arr[3])
CountUntil = int(arr[4])
#variable for error check
error = False
#checking for errors
if X < 1 or X > 9 or Y < 1 or Y > 9:
print "X and Y must be between 1 and 9"
error = True
if jump == 0:
print "jump cannot be 0"
error = True
elif (CountUntil - CountFrom) % jump != 0:
print "cannot jump from %d to %d",CountFrom,CountUntil
error = True
if error:
exit()
if CountFrom < 0 and CountUntil < 0 and jump > 0:
jump = jump * (-1)
herestring = StatString()
while CountFrom != CountUntil:
BoomTrach(CountFrom,X,Y)
CountFrom = CountFrom + jump
if(CountFrom != CountUntil):
herestring.outstring = herestring.outstring + ","
print herestring.outstring
error message: (the second 1 was marked as the source of the error)
>>> 1 1 1 1 1
SyntaxError: invalid syntax
>>>
I know what happened. You just run this module, and you thought that you start running it from the main function (like in C for example).
There is no problem with the raw_input line (the first line of the input). The problem is that your module does not try to read anything! You just typed "1 1 1 1 1" which is of course syntax error...
Append to your code this line to run the main function:
main()
You can also write the code of the main function not in some function, to get the same effect.