'>' not supported between instances of 'int' and 'str' - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: '>' not supported between instances of 'int' and 'str' (/thread-4568.html) Pages:
1
2
|
'>' not supported between instances of 'int' and 'str' - mp3909 - Aug-27-2017 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]) RE: '>' not supported between instances of 'int' and 'str' - Larz60+ - Aug-27-2017 you cannot compare integers to string. you need to make both sides the same type, like if len(word) > len(col[i][1]): RE: '>' not supported between instances of 'int' and 'str' - Bass - Aug-27-2017 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 RE: '>' not supported between instances of 'int' and 'str' - ichabod801 - Aug-27-2017 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)). RE: '>' not supported between instances of 'int' and 'str' - wavic - Aug-27-2017 You do not need nested loops: for i , word in enumerate(tableData): if len(word) > col[i][1]: # etc. RE: '>' not supported between instances of 'int' and 'str' - mp3909 - Aug-27-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? RE: '>' not supported between instances of 'int' and 'str' - ichabod801 - Aug-27-2017 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.
RE: '>' not supported between instances of 'int' and 'str' - mp3909 - Aug-27-2017 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 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 Hey, i got it now. you was right. i just started out with a list of empty strings. RE: '>' not supported between instances of 'int' and 'str' - mp3909 - Aug-27-2017 sorry, but here's another question. Can someone please explain why whenever I try to initialise col as col=[[0,0]]*3and then I do col[0][1]=3this then makes col[1][1]=3and col[2][1]=3? Shouldn't col[1][1]=0and col[2][1]=0because I only changed the value of col[0][1]=3 RE: '>' not supported between instances of 'int' and 'str' - ichabod801 - Aug-27-2017 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)] |