Getting TypeError:list indices must be integers
Hi, Please check out the following loop,here indexList1 and indexList2 are a list of numbers. for index1 in indexList1: for index2 in indexList2: if ti1[index1] == ti2[index2] and not index1 != indexList1.pop(): index1+=1 index2+=1 continue elif index1 == indexList1.pop() and charList in pairList: k = string2.index(char2[0]) instance = ti2(k) tiNew = ti1.append(instance) tiNewList.append(tiNew) else: break On running my program, python gives me a TypeError saying: if ti1[index1] == ti2[index2] and not index1 != indexList1.pop(): TypeError: list indices must be integers Even though index1 and index2 are really integers.Please help! -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting TypeError:list indices must be integers
On 13/06/2006 4:11 PM, Girish Sahani wrote: [snip] instance = ti2(k) tiNew = ti1.append(instance) ti2 is quacking function but ti1 is quacking list. Possibilities: (1) You meant to type ti2[k] ... and this section of code has not yet been executed, and would have featured as episode N+1 had morbid curiosity not led me to read further. (2) You have the weirdest system of choosing names that I have seen for decades. (3) Both of the above. Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Getting TypeError:list indices must be integers: apology
Hi ppl, I'm really sorry for the previous post. I write mails very quickly and end up making errors in it. This time i ended up giving a code portion from an old copy of my program. Here's the code portion that is giving a TypeError:list indices must be integers for index1 in indexList1: for index2 in indexList2: if ti1[index1] == ti2[index2] and index1 != indexList1[-1]: index1+=1 index2+=1 continue elif index1 == indexList1[-1] and charList in pairList: k = string2.index(char2[0]) instance = ti2(k) tiNew = ti1.append(instance) tiNewList.append(tiNew) else: break -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting TypeError:list indices must be integers
On 13/06/2006 4:11 PM, Girish Sahani wrote: [snip] instance = ti2(k) tiNew = ti1.append(instance) ti2 is quacking function but ti1 is quacking list. Possibilities: (1) You meant to type ti2[k] ... and this section of code has not yet been executed, and would have featured as episode N+1 had morbid curiosity not led me to read further. That is corrected. I'm appending a particular element of ti2 to ti1. It hasnt been executed because i'm stuck on that TypeError since 2 hours :( (2) You have the weirdest system of choosing names that I have seen for decades. :(( (3) Both of the above. Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting TypeError:list indices must be integers
On 13/06/2006 5:08 PM, John Machin wrote: On 13/06/2006 4:11 PM, Girish Sahani wrote: [snip] instance = ti2(k) tiNew = ti1.append(instance) ti2 is quacking function but ti1 is quacking list. Possibilities: (1) You meant to type ti2[k] ... and this section of code has not yet been executed, and would have featured as episode N+1 had morbid curiosity not led me to read further. (2) You have the weirdest system of choosing names that I have seen for decades. (3) Both of the above. Episode N+2: tiNew = ti1.append(instance) doesn't do what you think it does: foo = [9, 8, 7] bar = 'xyz' bar = foo.append(42) foo [9, 8, 7, 42] repr(bar) 'None' Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting TypeError:list indices must be integers
On 13/06/2006 5:33 PM, Girish Sahani wrote: Python prints 'c','c' when i print repr(index1),repr(index2).This means they are characters right?? But i have defined indexList as follows (and also tested the output, both are outputted as lists of numbers): for char in list1: i = substring1.index(c) That would be char, I presume, not c. indexList1.append(i) for char in list2: j = substring2.index(char) indexList2.append(j) (substring1 and substring2 are 2 different strings) Then i'm iterating over indexList1 and indexList2, so i fail to understand why i am getting the typeError On 13/06/2006 5:08 PM, John Machin wrote: On 13/06/2006 4:11 PM, Girish Sahani wrote: [SNIPPED] Girish, Could we please abide by the Geneva Convention: 1. Please don't top-post. 2. Please don't type what you thought was in your code; copy/paste actual most-recently-executed code. 3. Please reply to the newsgroup/mailing-list -- I've taken the liberty of dragging this back there as there appears to be no private content ... OK, so you've found that index1 and index2 each contain 'c'. Despite your belief that they should contain the results of some_string.index(some_char), the only reasonable hypothesis is that somebody is polluting the water further upstream. Who is that somebody? The usual and only suspect is *you*. Please go away and sprinkle print statements at likely spots further upstream until you have found the problem. Kindest possible regards, John -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting TypeError:list indices must be integers
Girish Sahani wrote: On 13/06/2006 4:11 PM, Girish Sahani wrote: [snip] instance = ti2(k) tiNew = ti1.append(instance) ti2 is quacking function but ti1 is quacking list. Possibilities: (1) You meant to type ti2[k] ... and this section of code has not yet been executed, and would have featured as episode N+1 had morbid curiosity not led me to read further. That is corrected. I'm appending a particular element of ti2 to ti1. It hasnt been executed because i'm stuck on that TypeError since 2 hours :( (2) You have the weirdest system of choosing names that I have seen for decades. :(( (3) Both of the above. Cheers, John How about just inserting some print statements that show you the type and value of each index before you use it. It is old fashioned, but it actually works. -Larry Bates -- http://mail.python.org/mailman/listinfo/python-list