生成一个Python脚本,统计列表中数据的最大最小值,平均值,以及大于某个外部给定值的数据个数,调用statistic需要调用什么函数库
时间: 2023-03-16 19:44:06 浏览: 115
可以使用Python标准库中的statistics模块。获取列表中数据的最大最小值,平均值可以使用statistics模块的max(),min(),mean()函数。获取大于某个外部给定值的数据个数可以使用count()函数。
相关问题
excel宏统计最大值、最小值、平均值和某几个区间值的占比
可以通过编写Excel VBA宏来实现此功能。以下是一个示例宏,可以统计选定范围内的最大值、最小值、平均值以及给定区间内数值的占比。
```VBA
Sub Statistic()
'定义变量
Dim rng As Range
Dim max As Double
Dim min As Double
Dim avg As Double
Dim count As Integer
Dim p1 As Integer
Dim p2 As Integer
Dim p3 As Integer
'获取选定范围
Set rng = Application.Selection
'计算最大值、最小值、平均值
max = WorksheetFunction.Max(rng)
min = WorksheetFunction.Min(rng)
avg = WorksheetFunction.Average(rng)
'计算区间内数值的占比
For Each cell In rng
If cell.Value >= 0 And cell.Value < 60 Then
p1 = p1 + 1
ElseIf cell.Value >= 60 And cell.Value < 80 Then
p2 = p2 + 1
ElseIf cell.Value >= 80 And cell.Value <= 100 Then
p3 = p3 + 1
End If
Next cell
'输出结果
count = rng.Count
MsgBox "最大值:" & max & vbNewLine & _
"最小值:" & min & vbNewLine & _
"平均值:" & avg & vbNewLine & _
"0-59分占比:" & Format(p1 / count, "0.00%") & vbNewLine & _
"60-79分占比:" & Format(p2 / count, "0.00%") & vbNewLine & _
"80-100分占比:" & Format(p3 / count, "0.00%")
End Sub
```
在Excel中执行宏的步骤:
1. 打开Excel文件,按键盘上的“Alt + F11”组合键,打开VBA编辑器;
2. 在VBA编辑器中,选择“插入”菜单下的“模块”,在新建的模块中粘贴上述代码;
3. 返回Excel界面,在要统计的数据区域选中一段区域,然后按键盘上的“Alt + F8”组合键,选择刚才新建的宏,点击“运行”即可。
请写出flink统计文档某一列的最大值最小值以及平均值打包代码
好的,以下是flink统计文档某一列的最大值最小值以及平均值打包代码,假设文档有三列,分别为col1、col2、col3,我们想要统计col2列的最大值、最小值和平均值:
```java
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple3;
public class Statistic {
public static void main(String[] args) throws Exception {
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
DataSet<Tuple3<String, Double, String>> input = env.readCsvFile("document.csv")
.ignoreFirstLine()
.parseQuotedStrings('"')
.ignoreInvalidLines()
.types(String.class, Double.class, String.class);
DataSet<Double> max = input
.aggregate(new MaxAggregate())
.map(new SelectMax());
DataSet<Double> min = input
.aggregate(new MinAggregate())
.map(new SelectMin());
DataSet<Double> avg = input
.aggregate(new AvgAggregate())
.map(new SelectAvg());
max.print();
min.print();
avg.print();
env.execute();
}
public static class MaxAggregate implements AggregateFunction<Tuple3<String, Double, String>, Double, Double> {
@Override
public Double createAccumulator() {
return Double.MIN_VALUE;
}
@Override
public Double add(Tuple3<String, Double, String> value, Double accumulator) {
return Math.max(value.f1, accumulator);
}
@Override
public Double getResult(Double accumulator) {
return accumulator;
}
@Override
public Double merge(Double a, Double b) {
return Math.max(a, b);
}
}
public static class MinAggregate implements AggregateFunction<Tuple3<String, Double, String>, Double, Double> {
@Override
public Double createAccumulator() {
return Double.MAX_VALUE;
}
@Override
public Double add(Tuple3<String, Double, String> value, Double accumulator) {
return Math.min(value.f1, accumulator);
}
@Override
public Double getResult(Double accumulator) {
return accumulator;
}
@Override
public Double merge(Double a, Double b) {
return Math.min(a, b);
}
}
public static class AvgAggregate implements AggregateFunction<Tuple3<String, Double, String>, Tuple3<Double, Double, Integer>, Tuple3<Double, Double, Double>> {
@Override
public Tuple3<Double, Double, Integer> createAccumulator() {
return new Tuple3<>(0d, 0d, 0);
}
@Override
public Tuple3<Double, Double, Integer> add(Tuple3<String, Double, String> value, Tuple3<Double, Double, Integer> accumulator) {
return new Tuple3<>(accumulator.f0 + value.f1, accumulator.f1 + value.f1, accumulator.f2 + 1);
}
@Override
public Tuple3<Double, Double, Double> getResult(Tuple3<Double, Double, Integer> accumulator) {
return new Tuple3<>(accumulator.f0, accumulator.f1, accumulator.f0 / accumulator.f2);
}
@Override
public Tuple3<Double, Double, Integer> merge(Tuple3<Double, Double, Integer> a, Tuple3<Double, Double, Integer> b) {
return new Tuple3<>(a.f0 + b.f0, a.f1 + b.f1, a.f2 + b.f2);
}
}
public static class SelectMax implements ReduceFunction<Double> {
@Override
public Double reduce(Double value1, Double value2) throws Exception {
return Math.max(value1, value2);
}
}
public static class SelectMin implements ReduceFunction<Double> {
@Override
public Double reduce(Double value1, Double value2) throws Exception {
return Math.min(value1, value2);
}
}
public static class SelectAvg implements ReduceFunction<Double> {
@Override
public Double reduce(Double value1, Double value2) throws Exception {
return value1 + value2;
}
}
}
```
在上面的代码中,我们定义了三个聚合函数,分别是`MaxAggregate`、`MinAggregate`和`AvgAggregate`,用于统计最大值、最小值和平均值。其中,`MaxAggregate`和`MinAggregate`实现了`AggregateFunction`接口,`AvgAggregate`实现了`AggregateFunction`接口,并且返回的结果是一个三元组,其中第一个元素是所有值的总和,第二个元素是所有值的个数,第三个元素是平均值。
我们还定义了三个`ReduceFunction`,分别是`SelectMax`、`SelectMin`和`SelectAvg`,用于在得到所有结果后从中选出最大值、最小值和平均值。最后,我们将统计结果打印出来。
阅读全文