简体   繁体   English

创建100个随机整数的列表,返回最大值

[英]Create List of 100 Random Integers, Return Max Value

Write a Python function that will take a the list of 100 random integers between 0 and 1000 and return the maximum value. 编写一个Python函数,该函数将获取0到1000之间的100个随机整数的列表,并返回最大值。 (Note: there is a builtin function named max but pretend you cannot use it.) (注意:有一个名为max的内置函数,但假装您不能使用它。)

Here's what I tried: 这是我尝试过的:

import random

list = []
for i in range(100):
    list.append(random.randint(0,1000))          

def max(list):
    #sort list from least to greatest
    answer = list.sort()
    #get last item in list (max value)
    list.pop()

    return max  

print (max(list))

As you can see, what I'm confused about is how to correctly use the sort and pop methods to return the max value within the max function. 如您所见,我感到困惑的是如何正确使用sort和pop方法来返回max函数中的最大值。 I'm currently getting: 我目前正在:

ParseError: bad input on line 12

Which is this line: 这是哪一行:

list.pop()

Not sure how to correct this. 不知道如何解决这个问题。 Thanks. 谢谢。

1) Your indentation was off 1)您的缩进已关闭

2) You managed to overwrite 2 Python builtins in 4 lines of code ( max() and list() )) 2)您设法用4行代码( max()list() )覆盖了2个Python内置函数

3) my_list.sort() does not return a list. 3) my_list.sort()不返回列表。 It operates in place sorting the list in-place. 它在原地对列表进行排序。 On the contrary, sorted(my_list) does return a list so you can either do my_list.sort() or my_list = sorted(my_list) . 相反, sorted(my_list)确实会返回一个列表,因此您可以执行my_list.sort()my_list = sorted(my_list)

4) Your return was just wrong. 4)您的return是错误的。

See the code below: 请参见下面的代码:

a_list = []
for i in range(100):
    a_list.append(random.randint(0, 1000))

def my_max(my_list):
    return sorted(my_list)[-1]  # here, the list we passed gets sorted and its last item is retrieved (item with index -1)

print(my_max(a_list))

Other interesting answers (hats off to @Rob) that feature a bit of cockiness: 其他有趣的答案(对@Rob表示感谢)有些自大:

def my_max(my_list):
    return min(l, key:lambda i: 1/i)

or 要么

def my_max(my_list):
    return min(l, key:lambda i: -i)

my_list.sort() sorts the list itself. my_list.sort()对列表本身进行排序。 If you want to store your sorted list in answer , you should use: 如果要将排序列表存储在answer ,则应使用:

answer = sorted(my_list)

You can also use a list comprehension to generate your first list as follows: 您还可以使用列表推导来生成您的第一个列表,如下所示:

>>> random
>>> 
>>> my_list = [random.randint(0, 1000) for i in range(100)]
>>> answer = sorted(my_list)
>>> answer.pop()
996  # This should be different each time

Now, your function can be: 现在,您的功能可以是:

def max(my_list):
    # sort list from least to greatest
    answer = sorted(my_list)
    # get last item in list (max value)
    max = answer.pop()

    return max

If you still want to use the same list, you can do: 如果仍然要使用相同的列表,则可以执行以下操作:

my_list.sort()
max = my_list.pop()

Note that: I prefer to call the list my_list because list is a python keyword. 请注意:我更喜欢将列表称为my_list因为list是python关键字。

I usually try to avoid using names that are have a predefined function / built-in names, this helps to avoid problems quite often. 我通常会尝试避免使用具有预定义函数/内置名称的名称,这有助于避免出现很多问题。 Like this it should work. 这样它应该可以工作。

import random

my_list = []
for i in range(100):
    my_list.append(random.randint(0,1000))          

def my_max(my_list):
    #sort list from least to greatest
    my_list.sort()
    #get last item in list (max value)
    my_max = my_list.pop()

    return my_max

print my_max(my_list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM