简体   繁体   English

如何从计算中排除列表的最大数量

[英]How to exclude the biggest number of a list from calculations

I have a list of lists with floats and want to get the mean of all the items of a list, but always leave the biggest number out.我有一个带有浮点数的列表列表,想要获取列表中所有项目的平均值,但始终将最大的数字排除在外。 The one float that was not included should then be subtracted from the mean since this would be the biggest discrepancy.然后应该从平均值中减去一个未包括的浮点数,因为这将是最大的差异。 Then I want to create a list to put the discrepancy in.然后我想创建一个列表来放置差异。

original_list = [[0.5, 1.4, 2.1, 5.2], [2.3, 3.2, 5.3, 2.1], [1.3, 3.1, 2.1, 5.3]]
result_list = [[-3.87], [-2.77], [-3.13]] 

I don't know how to exclude the biggest number from calculations because it's always in another position.我不知道如何从计算中排除最大的数字,因为它总是处于另一个位置。

We can loop over the list, sort each sublist and then perform the calculation, appending the result to a result list.我们可以遍历列表,对每个子列表进行排序,然后执行计算,将结果附加到结果列表中。

import numpy as np
original_list = [[0.5, 1.4, 2.1, 5.2], [2.3, 3.2, 5.3, 2.1], [1.3, 3.1, 2.1, 5.3]]

rv = []

for x in original_list:
    l = sorted(x)
    rv.append([np.mean(l[:-1])-l[-1]])

Output:输出:

>>> rv
[[-3.866666666666667], [-2.766666666666666], [-3.1333333333333333]]

To round the values use rv.append([round(np.mean(l[:-1])-l[-1], 2)]) :要舍入这些值,请使用rv.append([round(np.mean(l[:-1])-l[-1], 2)])

>>> rv
[[-3.87], [-2.77], [-3.13]]

Pure python solutions纯python解决方案

original_list = [[0.5, 1.4, 2.1, 5.2], [2.3, 3.2, 5.3, 2.1], [1.3, 3.1, 2.1, 5.3]]

def mean_no_max(l):
    s, m = 0, float('-inf')
    for i in l:
        s += i
        m = m if m > i else i
    return [(s - m) / (len(l) - 1) - m]

print(list(map(mean_no_max, original_list)))  # -> [[-3.86], [-2.76], [-3.13]]

Fun approach with single iteration over l using max() and sum()使用max()sum()l进行单次迭代的有趣方法

def mean_no_max(l):
    s = [0]
    def update_sum(x):
        s[0] += x
        return x
    max_value = max(l, key=update_sum)
    return (s[0] - max_value) / (len(l) - 1) - max_value

def mean_no_max(l):
    class Adder:
        def __init__(self, num=0, max_value=float('-inf')):
            self.num = num
            self.max_value = max_value
        def __add__(self, other):
            self.num += other
            if other > self.max_value:
                self.max_value = other
            return self
    adder = sum(l, Adder())
    return (adder.num - adder.max_value) / (len(l) - 1) - adder.max_value

as comprehension作为理解

from numpy import mean

original_list = [[0.5, 1.4, 2.1, 5.2], [2.3, 3.2, 5.3, 2.1], [1.3, 3.1, 2.1, 5.3]]

result_list = [[round(mean([v for v in list_ if v !=max(list_)])-max(list_), 2)] for list_ in original_list]
print(result_list)

One line:一条线:

import numpy as np
res = [[np.mean(sorted(i)[:-1])-sorted(i)[-1]] for i in original_list]
print(res)

you can get first a list with all the max values from your nested lists and then filter the nested lists by max values您可以先从嵌套列表中获取包含所有最大值的列表,然后按最大值过滤嵌套列表

max_values = [max(l) for l in original_list]
with_no_max = [[e for e in l if e < m] for l, m in zip(original_list, max_values)]

print(max_values)
print(with_no_max)

output:输出:

[5.2, 5.3, 5.3]
[[0.5, 1.4, 2.1], [2.3, 3.2, 2.1], [1.3, 3.1, 2.1]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM