Python Forum

Full Version: '>' not supported between instances of 'int' and 'str'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi,

Can someone please explain why if len(word) > col[i][1] is giving me an error saying '>' not supported between instances of 'int' and 'str'

Here is my code and the function takes this list tableData = [['apples', 'oranges', 'cherries', 'banana'],['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']] as an argument.

def myFunction(myData):
	col = [[0,0],[0,0],[0,0]]
	for i in range(len(tableData)):
		for word in tableData[i]:
			if len(word) > col[i][1]:
				col[i][1]=word
		print(col[i][1])
you cannot compare integers to string.
you need to make both sides the same type, like
if len(word) > len(col[i][1]):
My fault - Larz60+ answer is the right one, please ignore my response.

Hi,

It looks as though you are trying to compare two values, where one is a number (int) and the other is text (str).

I have had a quick look at the code but need to make some changes to include the variabes being passed to the function.

If the two variables are actually numeric, there still may be a problem due to the fact that one of them is being stored as a string.

Try defining both variables as numircs by putting int( ) around them, i.e. int(variable-name) and see how that goes.

Good luck and let me know how it goes...

Bass
I think the problem here is that col starts out as a list of lists of integers, and then gets changed into a list of lists of strings (line 6). You should either start it out as strings ([['', ''], ...]) or keep it as integers (col[i][1] = len(word)).
You do not need nested loops:

for i , word in enumerate(tableData):
    if len(word) > col[i][1]:
        # etc.
col starts out as list of integers.
len(word) gives an integer.
at the stage if len(word) > col[0][i] both sides are integers no?
At the beginning of the for word loop, yes. But then it gets replaced with a string in the first iteration of that loop, so on the second iteration you get the error.
yep but in the second iteration of the for loop i=1 and col[i][1] i.e. col[1][1] still contains an integer, no?

(Aug-27-2017, 01:24 PM)Larz60+ Wrote: [ -> ]you cannot compare integers to string.
you need to make both sides the same type, like
if len(word) > len(col[i][1]):

you cannot put len(col[i][1]) as that gives an error object of type 'int' has no len()

(Aug-27-2017, 02:11 PM)ichabod801 Wrote: [ -> ]At the beginning of the for word loop, yes. But then it gets replaced with a string in the first iteration of that loop, so on the second iteration you get the error.

Hey, i got it now. you was right. i just started out with a list of empty strings.
sorry, but here's another question.
Can someone please explain why whenever I try to initialise col as
col=[[0,0]]*3
and then I do
col[0][1]=3
this then makes
col[1][1]=3
and
col[2][1]=3
? Shouldn't
col[1][1]=0
and
col[2][1]=0
because I only changed the value of
col[0][1]=3
It's a property of lists in python, which are mutable. When you do col=[[0,0]]*3, you are not creating a list of three two item lists, you are creating a list of three pointers to one two item list. Since they are all pointing to the same thing, when you change it, all three of them report the change back.

When building lists like that, either do it explicitly, or do it with a list comprehension:

col = [[0, 0] for row in range(3)]
Pages: 1 2