Python Forum
'>' not supported between instances of 'int' and 'str'
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'>' not supported between instances of 'int' and 'str'
#1
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])
Reply
#2
you cannot compare integers to string.
you need to make both sides the same type, like
if len(word) > len(col[i][1]):
Reply
#3
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

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#4
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)).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
You do not need nested loops:

for i , word in enumerate(tableData):
    if len(word) > col[i][1]:
        # etc.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
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?
Reply
#7
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
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.
Reply
#9
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
Reply
#10
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)]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort Function: <' not supported between instances of 'float' and 'tuple' quest 2 345 Apr-30-2021, 07:37 PM
Last Post: quest
Exclamation TypeError: '>=' not supported between instances of 'int' and 'str' helpme1 11 1,204 Mar-11-2021, 11:13 AM
Last Post: helpme1
  Type error: '>' not supported between instances of 'NoneType' and 'int' spalisetty06 1 2,241 Apr-29-2020, 06:41 AM
Last Post: buran
  TypeError: '<' not supported between instances of 'str' and 'int' Svensation 5 2,739 Jan-20-2020, 08:12 PM
Last Post: buran
  TypeError: Not supported between instances of 'function' and 'int' palladium 9 7,702 Dec-06-2019, 12:40 AM
Last Post: palladium
  TypeError: '>=' not supported between instances of 'str' and 'int' AsadZ 8 3,490 Aug-20-2019, 11:45 AM
Last Post: ThomasL
  '>' not supported between instances of 'str' and 'int' graham23s 2 1,832 May-11-2019, 07:09 PM
Last Post: micseydel
  Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int sr12 8 6,246 Apr-11-2019, 08:19 PM
Last Post: sr12
  '<' not supported between instances of 'str' and 'int' jayaherkar 1 5,076 Apr-09-2019, 03:25 PM
Last Post: perfringo
  TypeError: '<' not supported between instances of 'int' and 'builtin_function_or_meth yann2771 1 2,827 Mar-05-2019, 09:51 PM
Last Post: stranac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020