从 Python 中的列表中获取数据范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32152233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:06:35  来源:igfitidea点击:

Getting the range of data from a list in Python

pythonlistrangemaxmin

提问by Ninja

I have a set of data that is in a list. I am not sure how to make a function which can take the range of that data and return the min and max values in a tuple.

我有一组列表中的数据。我不确定如何创建一个函数,该函数可以获取该数据的范围并返回元组中的最小值和最大值。

data:

数据:

[1,3,4,463,2,3,6,8,9,4,254,6,72]

my code at the moment:

我现在的代码:

def getrange(data):
    result=[]
    if i,c in data:
        range1 = min(data)
        range2 = max(data)
        result.append(range1, range2)
    return result 

采纳答案by Austin A

This is a very straight forward question and you're very close. If what I have below isn't correct, then please edit your question to reflect what you would like.

这是一个非常直接的问题,您非常接近。如果我下面的内容不正确,请编辑您的问题以反映您想要的内容。

Try this.

尝试这个。

def minmax(val_list):
    min_val = min(val_list)
    max_val = max(val_list)

    return (min_val, max_val)


Semantics

语义

I have a set of data that is in a list.

我有一组列表中的数据。

Be careful here, you're using python terms in a contradictory manner. In python, there are both sets and lists. I could tell you meant list here but you could confuse people in the future. Remember, in python sets, tuples, and lists are all different from one another.

在这里要小心,你以一种矛盾的方式使用 python 术语。在python中,有集合和列表。我可以告诉你这里的意思是列表,但你将来可能会混淆人们。请记住,在 Python 中,集合、元组和列表都彼此不同。

Here are the differences (taken from BlackHyman's comment below)

以下是差异(摘自下面 BlackHyman 的评论)

Data Type | Immutable | Ordered | Unique Values
===============================================
  lists   |    no     |   yes   |      no
  tuples  |    yes    |   yes   |      no
   sets   |    no     |   no    |      yes

Immutable - the data type can't be changed after instantiation.

不可变 - 实例化后无法更改数据类型。

Ordered - the order of the elements within the data type are persistent.

有序 - 数据类型中元素的顺序是持久的。

Unique Values - the data type cannot have repeated values.

唯一值 - 数据类型不能有重复值。

回答by Falko

I like NumPy's percentilefunction for the ability to get multiple percentiles at once:

我喜欢NumPy 的percentile功能,因为它能够一次获得多个百分位数:

import numpy as np
print np.percentile([1,3,4,463,2,3,6,8,9,4,254,6,72], [0, 100])

Output:

输出:

[   1.  463.]

(The minimum is the 0 % percentile; and the maximum is the 100 % percentile.)

(最小值为 0 % 百分位数;最大值为 100 % 百分位数。)

If you really need the result in a tuple, you can easily wrap it with tuple(...).

如果你真的需要元组中的结果,你可以很容易地用tuple(...).

回答by Richie Bendall

If You Are Looking To Get The Range Of The Numbers, You Can Use:

如果您想获得数字的范围,您可以使用:

def getrange(numbers):
    return max(numbers) - min(numbers)

I've Also Constructed This Code That You Can Use In Finding Averages:

我还构建了此代码,您可以使用它来查找平均值:

def average(numbers, type=None):
import statistics
try:
    statistics.mean(numbers)
except:
    raise RuntimeError('An Error Has Occured: List Not Specified (0018)')
if type == 'mean':
    return statistics.mean(numbers)
elif type == 'mode':
    return statistics.mode(numbers)
elif type == 'median':
    return statistics.median(numbers)
elif type == 'min':
    return min(numbers)
elif type == 'max':
    return max(numbers)
elif type == 'range':
    return max(numbers) - min(numbers)
elif type == None:
    return average(numbers, 'mean')
else:
    raise RuntimeError('An Error Has Occured: You Entered An Invalid Operation (0003)')

All You Need To Do Is Type average([1, 2, 3])To Get The Mean Average For 1, 2 And 3. For Other Commands, Do average([1, 2, 3, 'median')And This Will Give The Median Of The Numbers. You Can Change medianto: mean, mode, median, min, maxand range

您需要做的就是键入average([1, 2, 3])以获取 1、2 和 3 的平均平均值。对于其他命令,执行average([1, 2, 3, 'median')此操作将给出数字的中位数。您可以将中位数更改为:meanmodemedianminmaxrange

回答by Sevda

I just did this:

我只是这样做:

>>>data = [1,3,4,463,2,3,6,8,9,4,254,6,72]
>>>min_val = min(data)
>>>max_val = max (data)
>>>range_data = (min_val, max_val)
>>>print(range_data)
(1, 463)