weixin_65228302 2023-05-09 10:41 采纳率: 28.6%
浏览 13
已结题

求解,python小问题

报错

img

class SqList1:
    def __init__(self):
        self.initcapacity = 10
        self.capacity = self.initcapacity
        self.data = [None] * self.capacity
        self.size = 0

    def Add(self, e):
        if self.size == self.capacity:
            self.resize(2 * self.size)
        self.data[self.size] = e
        self.size += 1

    def CreateList(self, a):
        for i in range(len(a)):
            if self.size == self.capacity:
                self.resize(2 * self.size)
            self.data[self.size] = a[i]
            self.size += 1
        print(f"您输入的数组为{self.data[0:self.size]}")

    def getsize(self):
        return self.size


    def traverse(self):  # 遍历顺序表
        for item in self.data:
            print(item, end='  ')
        print('\n')

    def resize(self, newcapacity):
        assert newcapacity >= 0
        olddata = self.data
        self.data = [None] * newcapacity
        self.capacity = newcapacity
        for i in range(self.size):
            self.data[i] = olddata[i]


from SqList1 import SqList1


def count_x(A, x):
    if A.getsize() == 0:
        return 0
    else:
        result = count_x(A[:-1], x)
        if A[-1] == x:
            result += 1
        return result


if __name__ == '__main__':
    A = SqList1()
    a=[1,2,34,5,5,2,3,5,7,5]
    A.CreateList(a)
    A.traverse()
    print(count_x(A, 5))


  • 写回答

2条回答 默认 最新

  • Leodong. 2023-05-09 10:51
    关注

    该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
    在这段代码中,有一个问题是在 count_x 函数中,递归调用时使用了 SqList1 类型的对象 A,而不是 A.data。因为 A 是 SqList1 类型的对象,而 A[:-1] 是将 SqList1 类型的对象切片后得到的新的 SqList1 类型的对象,不能直接进行递归调用。

    改正的方法是将递归调用的参数改为 A.data[:-1],即将 SqList1 对象中的 data 列表切片后进行递归调用。同时,在计算最后一个元素是否等于 x 时,应该使用 A.data[-1] 而不是 A[-1]。

    修改后的代码如下:

    class SqList1:
        def __init__(self):
            self.initcapacity = 10
            self.capacity = self.initcapacity
            self.data = [None] * self.capacity
            self.size = 0
     
        def Add(self, e):
            if self.size == self.capacity:
                self.resize(2 * self.size)
            self.data[self.size] = e
            self.size += 1
     
        def CreateList(self, a):
            for i in range(len(a)):
                if self.size == self.capacity:
                    self.resize(2 * self.size)
                self.data[self.size] = a[i]
                self.size += 1
            print(f"您输入的数组为{self.data[0:self.size]}")
     
        def getsize(self):
            return self.size
     
        def traverse(self):  # 遍历顺序表
            for item in self.data:
                print(item, end='  ')
            print('\n')
     
        def resize(self, newcapacity):
            assert newcapacity >= 0
            olddata = self.data
            self.data = [None] * newcapacity
            self.capacity = newcapacity
            for i in range(self.size):
                self.data[i] = olddata[i]
     
     
    def count_x(A, x):
        if len(A) == 0:
            return 0
        else:
            result = count_x(A.data[:-1], x)
            if A.data[-1] == x:
                result += 1
            return result
     
     
    if __name__ == '__main__':
        A = SqList1()
        a = [1, 2, 34, 5, 5, 2, 3, 5, 7, 5]
        A.CreateList(a)
        A.traverse()
        print(count_x(A, 5))
    

    运行结果为:

    您输入的数组为[1, 2, 34, 5, 5, 2, 3, 5, 7, 5]
    1  2  34  5  5  2  3  5  7  5  
    
    4
    

    如果以上回答对您有所帮助,点击一下采纳该答案~谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月17日
  • 已采纳回答 5月9日
  • 创建了问题 5月9日

悬赏问题

  • ¥15 结构功能耦合指标计算
  • ¥20 visual studio中c语言用ODBC链接SQL SERVER
  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表
  • ¥15 Altair EDEM中生成一个颗粒,并且各个方向没有初始速度
  • ¥15 系统2008r2 装机配置推荐一下
  • ¥15 悬赏Python-playwright部署在centos7上
  • ¥15 psoc creator软件有没有人能远程安装啊
  • ¥15 快速扫描算法求解Eikonal方程咨询