迭代的同时删除,会导致索引变化,可以循环从后往前删除,或者将要删除的条目添加到另一个list,再删除。
在sort函数中的使用lambda进行条件排序导致list被置0
5本来写了一个def,想着dug一下,然后就发现问题了。
def DeleteArraylementsAccordingToPercentage(lorddata,percentage,name):
#按照百分比删除数组元素,并返回第二个数据
array = []
openpklfile = open(lorddata + '.pkl','rb')
start = pickle.load(openpklfile)
start.sort(start,key = lambda x:x[1])
lenth = len(start) * float(percentage)
for i in range(lenth):
del start[i]
savefile = open(name + '.pkl', 'wb')
pickle.dump(start, savefile)
savefile.close()
openpklfile.close()
- 点赞
- 写回答
- 关注问题
- 收藏
- 复制链接分享
- 邀请回答
3条回答
为你推荐
- 学习python lambda匿名函数的用法中难以理解的一条代码。
- python
- 2个回答
- sbx.set(secs)那里显示'NoneType' object has no attribute 'set',为什么不能用?
- python
- 1个回答
- Python中sort函数与其中Len()和Lambda的组合
- 开发语言
- list
- python
- 2个回答
- python题目:英文字符规律统计 答案里map()的功能不明白
- python
- 2个回答
- C++传入匿名表达式时出现的错误
- c++
- 1个回答
- List的sort()方法这样写对吗?
- arraylist
- lambda
- 6个回答
- 在sort函数中的使用lambda进行条件排序导致list被置0
- sort函数
- python
- 3个回答