python int对象是不可能的
我试图在Python程序中找到一个重复的号码,但是我得到一个错误int对象是不可能的
class Solution:
def duplicates(self, arr, n):
for i in arr:
a = arr.count(i)
if a >= 2:
return i
else:
return -1
if(__name__=='__main__'):
t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().strip().split()))
res = Solution().duplicates(arr, n)
for i in res:
print(i,end=" ")
print()
I am trying to find a duplicate number in the python program, but I am getting an error int object is not iterable
class Solution:
def duplicates(self, arr, n):
for i in arr:
a = arr.count(i)
if a >= 2:
return i
else:
return -1
if(__name__=='__main__'):
t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().strip().split()))
res = Solution().duplicates(arr, n)
for i in res:
print(i,end=" ")
print()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注意:我假设这是一个编程练习。到目前为止,这不是最有效的,也不是最快的编码方式来确定列表中是否有重复的项目。
此错误发生在此循环中:
查看
solution()。重复()
,res
始终是整数;不是整数列表,也不是包含单个整数的列表。您不能迭代它。您可以替换上述块:
Note: I'm assuming this is a programming exercise. This is by far not the most efficient nor the fastest-to-code way to figure out if there are duplicate items in a list.
The error occurs in this for loop:
Looking at
Solution().duplicates()
,res
will always be an integer; not a list of integers, nor a list containing a single integer. You cannot iterate over it.You could replace the block above with: