【发布时间】:2021-08-07 20:54:30
【问题描述】:
我正在尝试从数字列表中删除一个数字,但对于我的生活,我无法让它工作。
我尝试使用 list.remove() 方法和 .pop() 但它有些不起作用。最大是返回列表中最大数字的函数。我制作了列表的副本,因为问题要求的一部分是列表不被改变。当我尝试打印 bList 时,我得到了 None。
我还尝试通过引入变量 index = c.index(l) 来使用 .pop() 并使用也不起作用的 c.pop(index)。
def largest(aList):
theLargest = aList[0]
for i in range(1, len(aList)):
if theLargest < aList[i]:
theLargest = aList[i]
return theLargest
def main():
aList = [2,5,1,6]
c = aList.copy()
l = largest(c)
bList = c.remove(l)
print(bList)
main()
```
【问题讨论】:
-
或许可以试试:
bList = [x for x in aList if x != max(aList)]
标签: python arrays list function