Python:算作 int 但 python 认为是 str

Python: count it int but python thinks it is 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'

问题在行:

i = str(i)

所以稍后,当您为 one 分配新值时:

one = i

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

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