python - python list删除太近的数字
问题描述
我想使用一个列表作为 matplotlib 刻度,但是如果刻度太近,它将相互重叠。
所以最好去掉太接近的数字。例如:
distance = 10
x = [1,2,3,20,21,23,30,40,50]
f(x) = [1,20,30,40,50]
获得这种结果的最佳方法是什么?
解决方案
您可以循环列表以验证:
x = [1,2,3,20,21,23,30,40,50]
distance = 5
for index in range(len(x)-1 , 0 , -1):
if (x[index]-distance <= x[index-1] ):
x.remove(x[index])
print(x)
您可以根据自己的喜好更改距离,希望对您有所帮助
推荐阅读
- python-3.x - 处理 FileNotFoundError 的最佳方法
- processing - 通过球体表面上的位移映射处理输出高图
- performance - 将 CPU 和 GPU 与 OpenCL 相结合时的加速
- c++ - 运算符重载 () <<
- ios - Apple“强密码”建议破坏文本字段
- r - 如何在 R 中将 2019-01-01T00:00:00 转换为 2019-01-01 00:00:00
- r - 不同 R 包中定义的两个 S3 方法之间的命名空间冲突
- java - java.net.SocketException: socket failed: EPERM (Operation not allowed) 即使设置 usesCleartextTraffic 为 true
- kotlin - Kotlin Flow - 是否可以按需添加新值
- python - django rest框架,外键字段序列化理解困难