Delete Last Element

本文提供了一种使用Python方法删除列表中特定位置元素的技术,特别是删除最后一个元素及其更多元素的方法,避免了使用clearLists方法可能带来的不便。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

yo folks, I have an issue with clearing lists. In the current program which I'm coding, I have a method that clears a certain number of lists. This is rather inconvenient since during one part of the program where this method is used, it would be a lot more helpful if it only deleted the last elementfrom the lists. Is there anyway in which I can set index numbers as parameters to my method to solve this problem? 

The code for the method

def clearLists(self):
    del self.Ans[:]
    del self.masses[:]

Whenever I want to use this method, I merely write self.ClearLists() and it deletes every element in a list.

share improve this question
 
2 
I don't see the relation between the question title and body. –  fortran  Dec 2 '11 at 14:54
 
Sorry about that, that was a leftover from an old, unposted question, now fixxxed. –  user1036197  Dec 2 '11 at 14:59
3 
If you don't want to clear your lists, why would you call a method called clearLists? –  Wooble  Dec 2 '11 at 15:07
1 
Am I the only what that do not understand what are you looking for? –  Tadeck  Dec 2 '11 at 15:52

2 Answers

you can use lst.pop() or del lst[-1]

share improve this answer
 
 
that pop() method rocks, thanks –  armani  Nov 18 '14 at 22:49
 
pop() removes and returns the item, in case you don't want have a return use del ;) –  flacle  2 days ago

To delete the last element of the lists, you could use:

def deleteLast(self):
    if len(self.Ans) > 0:
        del self.Ans[-1]
    if len(self.masses) > 0:
        del self.masses[-1]
要求用C语言回答下面问题:给定一个顺序存储的线性表,请设计一个函数删除所有值大于min而且小于max的元素。删除后表中剩余元素保持顺序存储,并且相对位置不能改变。 函数接口定义: List Delete( List L, ElementType minD, ElementType maxD );其中List结构定义如下: typedef int Position; typedef struct LNode *List; struct LNode { ElementType Data[MAXSIZE]; Position Last; /* 保存线性表中最后一个元素在数组中的位置 */ };L是用户传入的一个线性表,其中ElementType元素可以通过>、==、<进行比较;minD和maxD分别为待删除元素的值域的下、上界。函数Delete应将Data[]中所有值大于minD而且小于maxD的元素删除,同时保证表中剩余元素保持顺序存储,并且相对位置不变,最后返回删除后的表。 裁判测试程序样例: #include <stdio.h> #define MAXSIZE 20 typedef int ElementType; typedef int Position; typedef struct LNode *List; struct LNode { ElementType Data[MAXSIZE]; Position Last; /* 保存线性表中最后一个元素的位置 */ }; List ReadInput(); /* 裁判实现,细节不表。元素从下标0开始存储 */ void PrintList( List L ); /* 裁判实现,细节不表 */ List Delete( List L, ElementType minD, ElementType maxD ); int main() { List L; ElementType minD, maxD; int i; L = ReadInput(); scanf("%d %d", &minD, &maxD); L = Delete( L, minD, maxD ); PrintList( L ); return 0; } /* 你的代码将被嵌在这里 */输入样例: 10 4 -8 2 12 1 5 9 3 3 10 0 4 输出样例: 4 -8 12 5 9 10 代码长度限制 16 KB 时间限制 200 ms 内存限制 64 MB
04-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值