Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果不使用最小值和最大值函数,如何查找最小值和最大值_Python_Python 3.x_List_Max_Min - Fatal编程技术网

Python 如果不使用最小值和最大值函数,如何查找最小值和最大值

Python 如果不使用最小值和最大值函数,如何查找最小值和最大值,python,python-3.x,list,max,min,Python,Python 3.x,List,Max,Min,我被要求在不使用Python中的min和max函数的情况下查找此列表的min和max。我不知道该怎么做。请帮忙 AList = 1,2,3,4 正如@figbeam所建议的,您可以对第一个值和最后一个值进行排序和检索,如下所示: AList = [1,2,3,4] sorted_list = sorted(AList) minValue = sorted_list[0] maxValue = sorted_list[-1] print (minValue) print (maxValue)

我被要求在不使用Python中的min和max函数的情况下查找此列表的min和max。我不知道该怎么做。请帮忙

AList = 1,2,3,4

正如@figbeam所建议的,您可以对第一个值和最后一个值进行排序和检索,如下所示:

AList = [1,2,3,4]

sorted_list = sorted(AList)
minValue = sorted_list[0]
maxValue = sorted_list[-1]

print (minValue)
print (maxValue)

如果示例不适合您,还有其他方法,例如复制列表,然后根据下一个条目迭代并重新分配最小值或最大值。

一种方法是使用for循环:

def get_min_max(numbers):
    if not all([type(number) in [int, float] for number in numbers]):
        raise TypeError('All items in numbers must be int or float.')

    min = None
    max = None
    for number in numbers:
        min = number if min is None or number < min else min
        max = number if max is None or number > max else max

    return min, max

如果您分享您已经尝试过的代码,并且我们指出您犯了哪些错误,这可能对您最有帮助。一般来说,我们避免无中生有地写家庭作业问题的答案。为什么要抄写?无论如何,我认为如果作业不考虑最小值和最大值,它可能不考虑排序,排序就不那么琐碎了……同意。这取决于排序是否无效。我会制作一个副本,只是为了迭代原始副本和副本,并以这种方式进行比较。可能不符合逻辑,但它以前做过。不知道我为什么这么做