473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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!

Jun 13 '06 #1
7 12702
On 13/06/2006 4:11 PM, Girish Sahani wrote:
Hi,
Please check out the following loop,here indexList1 and indexList2 are a
list of numbers.
Later you say they are integers. Note: Python calls 2.0 a float, not an
integer.
|>>> foo = [9, 8, 7]
|>>> foo[2.0]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list indices must be integers
|>>>

for index1 in indexList1:
for index2 in indexList2:
if ti1[index1] == ti2[index2] and not index1 != indexList1.pop():
Wouldn't
... and index1 == indexList1.pop()
be easier to follow?

Didn't I read earlier today somebody being admonished for modifying a
list over which they were iterating? Who was that? You! What do you
think indexList1.pop() is doing???
index1+=1
index2+=1
The above 2 statements have no effect at all. In particular, they
*don't* affect the next value used by the relevant for statement, if
that's what you were hoping for.
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!


How do you know that they are integers? Have you followed the advice
given by readers of previous episodes of your adventures, and inserted
print statements at relevant points?

In this case, inserting
print repr(index1), repr(index2)
immediately after the 2nd for statement would be a very good idea.

Please do come back and tell us what you find out, *and* what you have
learned.

HTH,
John
Jun 13 '06 #2
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
Jun 13 '06 #3
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
Jun 13 '06 #4
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
Jun 13 '06 #5
> 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


Jun 13 '06 #6
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
Jun 13 '06 #7
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
Jun 13 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Nicolas Girard | last post by:
Hi, Forgive me if the answer is trivial, but could you tell me how to achieve the following: {k1:,k2:v3,...} --> ,,,...] The subtle point (at least to me) is to "flatten" values that are...
51
by: Noam Raphael | last post by:
Hello, I thought about a new Python feature. Please tell me what you think about it. Say you want to write a base class with some unimplemented methods, that subclasses must implement (or...
8
by: Arvid Andersson | last post by:
Hello, I need to convert a string to a number, but the string can contain +,-,* and / as well as parenthesis. For example, if I have the string "30/(6+9)" I would like a function that returned the...
12
by: Georg Brandl | last post by:
Hello, in follow-up to the recent "dictionary accumulator" thread, I wrote a little module with several subclassed dicts. Comments (e.g. makes it sense to use super), corrections, etc.? Is...
5
by: Jan Danielsson | last post by:
Hello all, I have written a simple whiteboard application. In my application, I want to be able to set draw attributes. This part works. I have a dictionary object which contains stuff like:...
25
by: tsaar2003 | last post by:
Hi Pythonians, To begin with I'd like to apologize that I am not very experienced Python programmer so please forgive me if the following text does not make any sense. I have been missing...
42
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage:...
5
by: Maxim Veksler | last post by:
Hello list, I'm trying to subclass socket and select, for both I get: """ TypeError: Error when calling the metaclass bases module.__init__() takes at most 2 arguments (3 given) """, I don't...
0
by: Miles | last post by:
On Mon, Sep 15, 2008 at 6:06 AM, Harish K Vishwanath <harish.shastry@gmail.comwrote: "built-in type" generally means "implemented in C", also sometimes called "extension type". Both the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.