谁能帮我修改一下啊
class SqList: # 顺序表
def __init__(self): # 构建空表
self.elem = []
self.size = 0
def listAppend(self, item): # 插入元素item在表尾
self.elem.append(item)
def listInsert(self, index, i): # 在index之前插入元素i且递增
if self.elem[index] > i:
while self.elem[index] > i:
index -= 1
self.elem.insert(index + 1, i)
elif self.elem[index] < i:
while self.elem[index] < i:
index += 1
self.elem.insert(index, i)
else:
print("表不能有相同元素")
self.elem.insert(index - 1, i)
def listDelelte(self, index): # 删除第index元素
del self.elem[index - 1]
def dellast(self): # 删除最后一个元素
self.elem.pop()
def length(self): # 求表长
return len(self.elem)
def isEmpty(self): # 判断表空
return self.elem == []
def traverse(self): # 遍历顺序表
for item in self.elem:
print(item, end=' ')
print('\n')
def del_fushu(self,L):
i, j = 0, 0
while j < len(self):
if self[j] >= 0:
self[i] = self[j]
i += 1
j += 1
del self[i:]
return self
from SqList import SqList
if __name__ == '__main__':
L=SqList()
L.listAppend(1)
L.listAppend(2)
L.listAppend(-1)
L.listAppend(-2)
L.listAppend(3)
L.listAppend(-3)
L.traverse()
L=del_fushu(L)
L.traverse()
目的是想删除负数,且排序不变
可是老是报错,我基础也有点不好
while j < len(self):
TypeError: object of type 'SqList' has no len()
1 2 -1 -2 3 -3