[英]For loops vs Max function for finding the largest number in a list in Python (Beginner)
What is the difference between these both?这两者有什么区别? For loop and Max Function. Which one should I choose?
For循环和Max Function,我该选哪个? For loop:
对于循环:
numbers = [3, 6, 2, 8, 4, 10]
max = numbers[0]
for number in numbers:
if number > max:
max = number
print(max)
Instead, why can't I use the max function, which is only 1 line of code?相反,为什么我不能使用只有 1 行代码的最大 function? Max function:
最大 function:
print(max(3, 6, 2, 8, 4, 10))
Both of them show the same thing on the terminal.它们都在终端上显示相同的内容。 Why can't I choose this one?
为什么我不能选择这个?
You should use the max() function. It requires fewer lines of code, and it has a way, way smaller footprint than a for loop.您应该使用 max() function。它需要的代码行更少,而且它的占用空间比 for 循环小得多。 There are no cons to using the max() function, it will work exactly the same way as a for loop would, just with less lines of code.
使用 max() function 没有任何缺点,它的工作方式与 for 循环完全相同,只是代码行数更少。 Nothing should stop you from using this built-in method.
没有什么可以阻止您使用这个内置方法。 As far as optimization and speed go, I believe there won't be a significant difference using either.
至于优化和速度 go,我相信使用任何一个都不会有显着差异。
The max function runs faster usually than an equivalent for loop. max function 通常比等效的 for 循环运行得更快。 Running timeit, I get:
运行 timeit,我得到:
>>> import timeit
>>> print(min(timeit.Timer('max((1,2,3,4,5,6,7,8,9,10))').repeat(100,10000)))
0.0017686000001049251
>>> print(min(timeit.Timer('''max = 1
for number in (1,2,3,4,5,6,7,8,9,10):
if number > max:
max = number''').repeat(100,10000)))
0.0028327999998509767
The numbers printed are the execution times in seconds, for a minimum of 100 trials of 10000 repetitions of finding max both ways.打印的数字是以秒为单位的执行时间,至少要进行 100 次重复 10000 次查找最大值的试验。 As you can see, max is faster.
如您所见,max 更快。 The reason someone decided to use a for loop in a tutorial is probably to illustrate the idea of for loops to beginners.
有人决定在教程中使用 for 循环的原因可能是为了向初学者说明 for 循环的概念。
Suggest to use built-in max()
, it not only saves your coding time, but also less-error-prone and faster.建议使用内置的
max()
,它不仅可以节省您的编码时间,而且不易出错且速度更快。
Why not make life easier:)为什么不让生活更轻松:)
Of course, You will use max()
it is O(n) in term of Big O notation unless you are using a different data structure supporting the max of a value collection due to some implementation invariant.当然,您将使用
max()
在Big O 表示法方面它是 O(n) 除非您使用不同的数据结构来支持值集合的最大值,因为某些实现不变。
Finding a maximum element using a loop, it will iterate over all the element and that case loop also will take O(n).使用循环查找最大元素,它将遍历所有元素,并且 case 循环也将采用 O(n)。 If you have no limitation o using
max()
then you can use max()
which is easy and one linear.如果你没有使用
max()
的限制,那么你可以使用max()
,它很简单并且是线性的。
You can also go through about max()
efficiency .也可以通过 about
max()
效率go 。
When you use the max()
function, you're using a built-in function that comes with python. These so-called built-in functions are generally faster than if you coded a solution for that exact same task yourself.当您使用
max()
function 时,您使用的是 python 附带的内置 function。这些所谓的内置函数通常比您自己为完全相同的任务编写解决方案要快。
max()
takes as input an iterable
, meaning: a list, tuple, or just values separated by commas. max()
将一个iterable
作为输入,意思是:一个列表、元组,或者只是用逗号分隔的值。
In your code, you're trying to achieve the same thing as the max function, but it would only work when given a list or a tuple as input numbers
.在您的代码中,您试图实现与最大 function 相同的结果,但它仅在给定列表或元组作为输入
numbers
时才有效。 Look at these examples below:看看下面这些例子:
my_tuple = (2,5,6,1,6)
print (max(my_tuple))
>> 6
my_list = [1,2,3,3,8]
print (max(my_list))
>> 8
print (max(5,1,2,5,3,4))
>> 5
It should be useful to use implement the max()
function for most if not all scenarios.对于大多数(如果不是所有)场景,使用
max()
function 应该很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.