python - 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'
解决方案
问题在于:
i = str(i)
因此,稍后,当您为 分配新值时one
:
one = i
one
变成一个字符串。而不是改变i
,改变if
:
if (words.count(str(i)) > one):
推荐阅读
- php - 发送数组以进行删除时,允许的内存大小已用尽
- xml - 与使用 su 连接多个 XML 标记相关
- java - Codename One & SQLite 连续测试 & 接收 java.lang.UnsatisfiedLinkError org.sqlite.NativeDB._open(Ljava/lang/String;I)V 异常
- caffe - 如何修复“在图像中找不到 JPEG 数据”
- amazon-web-services - 如何使用 go-cloud s3blob 设置公共阅读?
- python - RobotFramework 'ASCII' 编解码器无法解码位置 1 中的字节 0xd7:序数不在范围内(128)
- java - 方法 'set(double)' 从未用于我的双变量,但它用于我的字符串变量 - 在这个 arraylist 类中
- reactjs - 使用自定义 Express 服务器时,端点返回 404
- instagram - instagram 的 query_hash 算法是什么?
- python - 从像素中获取 RGB 值