从列表中删除项目范围 [英] Removing range of items from a list
本文介绍了从列表中删除项目范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以删除列表中某个范围内的项目?例如:a = [1,2,3,4,5]
.如何从 3 删除到 5 ?
Is there a way to remove items in a range in list? For example: a = [1,2,3,4,5]
. How to remove items from 3 to 5?
推荐答案
这种方法应该可以解决问题
Something like this should do the trick
[z for z in [1,2,3,4,5,6,7] if not 3<=z<=5]
Out[2]:
[1, 2, 6, 7]
如果您想使其更加灵活,可以根据需要将其替换为变量,只需完成以下操作即可:
If you want to make it more flexible can replace with variables depending on your needs which is simply done:
alist=[1,2,3,4,5,6,7]
lowerbound=3
upperbound=5
resultlist=[z for z in alist if not lowerbound<=z<=upperbound]
#result you want stored as 'resultlist'
这篇关于从列表中删除项目范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文