首页 > 解决方案 > Python:将其计数为 int 但 python 认为它是 str

问题描述

我检查了“python”计数函数,发现它是整数,但是当我尝试检查它是否大于某个数字时,它告诉我它是 str

one = 0
two = 0
three = 0

for i in words:
    i = str(i)
    if (words.count(i) > one):
        one = i

    elif (words.count(i) > two):
        two = i

    elif (words.count(i) > three):
        three = i

错误:

    if (words.count(i) > one):
TypeError: '>' not supported between instances of 'int' and 'str'

标签: pythonstringcountint

解决方案


问题在于:

i = str(i)

因此,稍后,当您为 分配新值时one

one = i

one变成一个字符串。而不是改变i,改变if

if (words.count(str(i)) > one):

推荐阅读