Python:从数组列表中删除一系列数字

时间:2013-10-13 19:20:09

标签: python arrays matplotlib range

我在从数组列表中删除范围a到b中的元素时遇到问题。我在网上搜索的解决方案似乎只适用于单个元素,相邻元素和/或整数元素。我正在处理浮点数。

self.genx = np.arange(0, 5, 0.1)

temp_select = self.genx[1:3] #I want to remove numbers from 1 - 3 from genx
print(temp_select)
self.genx = list(set(self.genx)-set(temp_select))
print(self.genx)
plt.plot(self.genx,self.geny)

但是我在控制台中得到以下内容,这是因为我减去浮点数而不是整数,所以它实际上是减去而不是删除它在处理整数时会做什么:

genx:[0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0,2.1,2.1 ,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9  ,3.0,3.1,3.2,3.3,3.4,3.5,3.6,3.7,3.8,3.9,4.0,4.1,4.2,4.3,4.4 ,4.5,4.6,4.7,4.8,4.9]

temp_select:[0.1 0.2]

genx(扣除后):[0.0,0.5,2.0,3.0,4.0,1.5,1.0,1.1000000000000001,0.70000000000000007,0.90000000000000002,2.7000000000000002,0.30000000000000004,2.9000000000000004,1.9000000000000001,3.3000000000000003,0.40000000000000002,4.7000000000000002,3.4000000000000004,2.2000000000000002,2.8000000000000003, 1.4000000000000001,0.60000000000000009,3.6000000000000001,1.3,1.2000000000000002,4.2999999999999998,4.2000000000000002,4.9000000000000004,3.9000000000000004,3.8000000000000003,2.3000000000000003,4.8000000000000007,3.2000000000000002,1.7000000000000002,2.5,3.5,1.8,4.1000000000000005,2.4000000000000004,4.4000000000000004,1.6000000000000001,0.80000000000000004,2.6000000000000001,4.6000000000000005,2.1000000000000001, 3.1000000000000001,3.7000000000000002,4.5]

2 个答案:

答案 0 :(得分:0)

我没有对此进行测试,但您应该可以执行以下操作:

self.genx = [ item for item in self.genx if not range_min < item < range_max ]

答案 1 :(得分:0)

self.genx = [ item for item in self.genx if not range_min <= item <= range_max ]

这是你想要的吗?