博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delete Last Element
阅读量:4029 次
发布时间:2019-05-24

本文共 1466 字,大约阅读时间需要 4 分钟。

13
1

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.

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

2 Answers

35

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

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

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]

转载地址:http://gshbi.baihongyu.com/

你可能感兴趣的文章
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
jQuery性能优化指南
查看>>
Oracle 物化视图
查看>>
PHP那点小事--三元运算符
查看>>
解决国内NPM安装依赖速度慢问题
查看>>
Brackets安装及常用插件安装
查看>>
Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
查看>>
fastcgi_param 详解
查看>>
Nginx配置文件(nginx.conf)配置详解
查看>>
标记一下
查看>>
一个ahk小函数, 实现版本号的比较
查看>>
IP报文格式学习笔记
查看>>
autohotkey快捷键显示隐藏文件和文件扩展名
查看>>
Linux中的进程
查看>>
学习python(1)——环境与常识
查看>>
学习设计模式(3)——单例模式和类的成员函数中的静态变量的作用域
查看>>
自然计算时间复杂度杂谈
查看>>
当前主要目标和工作
查看>>