python返回列表中最大的整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17916293/
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 09:28:35  来源:igfitidea点击:

python return largest integer in the list

pythonlist

提问by user2627901

I am new to python and am trying to work with lists. how can i get my function to accept a list of integers and then returns the largest integer in the list?

我是 python 新手,正在尝试使用列表。我怎样才能让我的函数接受一个整数列表,然后返回列表中的最大整数?

回答by inspectorG4dget

Use the built-in maxfunction:

使用内置max函数

>>> L=[2,-7,3,3,6,2,5]
>>> max(L)
6

If you want to use maxin a custom function, you could do this:

如果你想max在自定义函数中使用,你可以这样做:

def getMaxOfList(L):
    return max(L)

I don't know why you would want to do this though, since it provides absolutely no new functionality

我不知道你为什么要这样做,因为它绝对没有提供新功能

If you want to write your own implementation of max:

如果您想编写自己的实现max

def myMax(L):
    answer = None
    for i in L:
        if i > answer:
            answer = i
    return answer

回答by Mandar Pande

use maxfunction:

使用max功能:

>>> L=[2,-7,3,3,6,2,5]
>>> L
[2, -7, 3, 3, 6, 2, 5]
>>> max(L)
6