Posts: 66
Threads: 28
Joined: Aug 2017
Aug-27-2017, 01:18 PM
(This post was last modified: Aug-27-2017, 01:19 PM by mp3909.)
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])
Posts: 12,027
Threads: 485
Joined: Sep 2016
you cannot compare integers to string.
you need to make both sides the same type, like
if len(word) > len(col[i][1]):
Posts: 58
Threads: 10
Joined: Mar 2017
Aug-27-2017, 01:30 PM
(This post was last modified: Aug-27-2017, 01:33 PM by Bass.)
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
Posts: 4,220
Threads: 97
Joined: Sep 2016
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)).
Posts: 2,953
Threads: 48
Joined: Sep 2016
You do not need nested loops:
for i , word in enumerate(tableData):
   if len(word) > col[i][1]:
       # etc.
Posts: 66
Threads: 28
Joined: Aug 2017
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?
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 66
Threads: 28
Joined: Aug 2017
Aug-27-2017, 02:30 PM
(This post was last modified: Aug-27-2017, 02:37 PM by mp3909.)
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.
Posts: 66
Threads: 28
Joined: Aug 2017
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
Posts: 4,220
Threads: 97
Joined: Sep 2016
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)]
|