如何获得最接近列表的值?
How to get the closest value to a list?
我有一个很大的列表,其中包含大量数据。如果 >= limit
.
,我正在尝试在该列表中获取 min
x[-2]
这是我的清单:enter link description here
这是我的代码:
limit = 45.7
ls_list = []
for key,sublists in itertools.groupby(my_list,lambda y:y[0]):
v = min(x for x in sublists if float(x[5].replace(',', '.')) >= limit);
ls_list.append(v[-2])
print(ls_list)
这是我的输出:
[137.332]
如果您查看 my_list
,您可以看到 x[5]
中的 45,705
应该
实际上是 min(>= limit)
所以它应该抓住那一行中的 x[-2]
。现在它抓住了整个列表的 x[-2]
...
预期输出为:
[148.419]
请注意,我使用 groupby
函数是因为最初
my_list
还包含来自 C150, C151, C152
等的值...
现在 min
函数从 x[0]
开始对列表进行排序。假设你有一个像这样的列表:
a = [['a', 'x', 'r', 5, 1],
['a', 'x', 'r', 2, 3],
['a', 'x', 'r', 4, 5],
['a', 'x', 'r', 3, 6]]
所以如果你 运行 min(a)
它将给出 a[1]
因为所有 sublists
中的所有元素在第 4 个元素之前都是相等的,所以 min
基于此元素。
在您的例子中,由于索引 0、1、2 和 3 处的子列表的所有元素完全相同,这就是为什么 min
returns 列表中具有最小值的原因第 4 个指标。结果是 my_list.
中的最后一个子列表
试试这个:
v = min((x for x in sublists if float(x[5].replace(',', '.')) >= limit), key=lambda x: x[-1])
上一行中的 key=lambda...
函数会做的是,它将强制 min
根据每个子列表的最后一个元素找到最小值。
或者你也可以只取你想要计算最小值的预期值,即:
v = min(x[-1] for x in sublists if float(x[5].replace(',', '.') >= limit)
但这会附加来自 x[5] 或 x[-1] 的值,而不是来自 x[-2] 的值。上述使用 lambda 的方法将给出预期的输出。
limit = 45.7
import math
[i[4] for i in my_list if math.isclose(float(i[5].replace(',','.')),limit, rel_tol=0.001)]
您可以使用具有 isclose()
方法的 math
模块
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
Return True if the values a and b are close to each other and False
otherwise.
Whether or not two values are considered close is determined according
to given absolute and relative tolerances.
rel_tol is the relative tolerance – it is the maximum allowed
difference between a and b, relative to the larger absolute value of a
or b. For example, to set a tolerance of 5%, pass rel_tol=0.05
输出:
[148.419]
我有一个很大的列表,其中包含大量数据。如果 >= limit
.
min
x[-2]
这是我的清单:enter link description here
这是我的代码:
limit = 45.7
ls_list = []
for key,sublists in itertools.groupby(my_list,lambda y:y[0]):
v = min(x for x in sublists if float(x[5].replace(',', '.')) >= limit);
ls_list.append(v[-2])
print(ls_list)
这是我的输出:
[137.332]
如果您查看 my_list
,您可以看到 x[5]
中的 45,705
应该
实际上是 min(>= limit)
所以它应该抓住那一行中的 x[-2]
。现在它抓住了整个列表的 x[-2]
...
预期输出为:
[148.419]
请注意,我使用 groupby
函数是因为最初
my_list
还包含来自 C150, C151, C152
等的值...
现在 min
函数从 x[0]
开始对列表进行排序。假设你有一个像这样的列表:
a = [['a', 'x', 'r', 5, 1],
['a', 'x', 'r', 2, 3],
['a', 'x', 'r', 4, 5],
['a', 'x', 'r', 3, 6]]
所以如果你 运行 min(a)
它将给出 a[1]
因为所有 sublists
中的所有元素在第 4 个元素之前都是相等的,所以 min
基于此元素。
在您的例子中,由于索引 0、1、2 和 3 处的子列表的所有元素完全相同,这就是为什么 min
returns 列表中具有最小值的原因第 4 个指标。结果是 my_list.
试试这个:
v = min((x for x in sublists if float(x[5].replace(',', '.')) >= limit), key=lambda x: x[-1])
上一行中的 key=lambda...
函数会做的是,它将强制 min
根据每个子列表的最后一个元素找到最小值。
或者你也可以只取你想要计算最小值的预期值,即:
v = min(x[-1] for x in sublists if float(x[5].replace(',', '.') >= limit)
但这会附加来自 x[5] 或 x[-1] 的值,而不是来自 x[-2] 的值。上述使用 lambda 的方法将给出预期的输出。
limit = 45.7
import math
[i[4] for i in my_list if math.isclose(float(i[5].replace(',','.')),limit, rel_tol=0.001)]
您可以使用具有 isclose()
方法的 math
模块
math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
Return True if the values a and b are close to each other and False otherwise.
Whether or not two values are considered close is determined according to given absolute and relative tolerances.
rel_tol is the relative tolerance – it is the maximum allowed difference between a and b, relative to the larger absolute value of a or b. For example, to set a tolerance of 5%, pass rel_tol=0.05
输出:
[148.419]