python int对象是不可能的

发布于 2025-02-11 04:04:18 字数 469 浏览 2 评论 0原文

我试图在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2025-02-18 04:04:18

注意:我假设这是一个编程练习。到目前为止,这不是最有效的,也不是最快的编码方式来确定列表中是否有重复的项目。

此错误发生在此循环中:

res = Solution().duplicates(arr, n)
for i in res:
    print(i,end=" ")

查看solution()。重复()res始终是整数;不是整数列表,也不是包含单个整数的列表。您不能迭代它。

您可以替换上述块:

res = Solution().duplicates(arr, n)
print(res, end=" ")

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:

res = Solution().duplicates(arr, n)
for i in res:
    print(i,end=" ")

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:

res = Solution().duplicates(arr, n)
print(res, end=" ")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文