Python: Removing a range of numbers from array list
我在从数组列表中删除范围A到B的元素时遇到问题。我在网上搜索的解决方案似乎只适用于单个元素、相邻元素和或整数元素。我在处理浮点数。
1 2 3 4 5 6 7 | 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.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条]
温度选择:[0.1 0.2]
genx(减去后):[0.0,0.5,2.0,3.0,4.0,1.5,1.0,1.1000000000001,0.7000000000000007,0.900000000000000000002,2.70000000000002,0.30000000000000004,2.900000000000004,1.900000000000001,3.30000000000000003,0.40000000000000000000002,4.700000000000000000002,3.4000000000000000004,2.20000000000000002,2.8000000000000000000000003,1.4000000000000000000,0.600000000000000 9、3.6000000000000001、1.3、1.200000000002、4.2999999999998、4.20000000000000002、4.900000000000004、3.900000000000004、3.800000000000003、2.30000000000000003、4.800000000000007、3.20000000000000002、1.700000000000002、2.5、3.5、1.8、4.10000000000000000000005、2.40000000000004、4.4000000000000000004、1.600000000000000000001、0.8000000000000004、2.6000000000000001、4.6000000000000005、2.100000000000000001、3.10000000000000001、3.700000000000002、4.5]
1 | self.genx = [ item for item in self.genx if not range_min <= item <= range_max ] |
这就是你想要的吗??
我没有测试这个,但是你应该能够做如下的事情:
1 | self.genx = [ item for item in self.genx if not range_min < item < range_max ] |