2010-06-22 84 views

回答

90

什么MAX()

highest = max(1, 2, 3) # or max([1, 2, 3]) for lists 
+2

谢谢,这是正是我需要的:d – 2010-06-22 04:03:19

+0

真棒,有乐趣的python-ING :-) – Jubal 2010-06-22 04:04:49

+1

他要求*组* – rbp 2016-01-14 17:43:05

6

使用max()

>>> l = [1, 2, 5] 
>>> max(l) 
5 
>>> 
10

您可以使用内置的功能max()使用多个参数:

print max(1, 2, 3) 

或列表:

list = [1, 2, 3] 
print max(list) 

或事实上任何可迭代的。

-4
#Ask for number input 
first = int(raw_input('Please type a number: ')) 
second = int(raw_input('Please type a number: ')) 
third = int(raw_input('Please type a number: ')) 
fourth = int(raw_input('Please type a number: ')) 
fifth = int(raw_input('Please type a number: ')) 
sixth = int(raw_input('Please type a number: ')) 
seventh = int(raw_input('Please type a number: ')) 
eighth = int(raw_input('Please type a number: ')) 
ninth = int(raw_input('Please type a number: ')) 
tenth = int(raw_input('Please type a number: ')) 

    #create a list for variables 
sorted_list = [first, second, third, fourth, fifth, sixth, seventh, 
       eighth, ninth, tenth] 
odd_numbers = [] 

    #filter list and add odd numbers to new list 
for value in sorted_list: 
    if value%2 != 0: 
     odd_numbers.append(value) 
print 'The greatest odd number you typed was:', max(odd_numbers) 
+2

1)没有理由不打算额外的东西my_list = sorted([int(raw_input('请输入数字'))for _ in xrange(10))。 2)你有一个名为sorted_list的列表,但你实际上没有对它进行排序3)这个问题中没有任何问题只是过滤出奇数4)这是什么规定以前的答案5年前没有(除了回答这个问题没有被问到,并且以一种不太优雅的方式) – Foon 2015-10-07 16:06:51

3

如果你必须要找到它,而无需使用最多的功能,那么你可以按照下面的代码:

a=[1,2,3,4,6,7,99,88,999] 
    max= 0 
    for i in a: 
     if i > max: 
      max=i 
    print(max) 
+0

为什么你不能使用max函数? – 2017-08-30 01:59:36

+0

因此,我为那些准备面试的人写了这篇文章,其中的问题是如何在不使用max函数的情况下找到最大值的列表。如果您觉得面试的好处,请upvote – 2017-08-30 03:05:24

0

max是Python中的内置函数,这是用来从一个序列获得最大的价值,即(列表,元组,集合等)

print(max([9, 7, 12, 5])) 

# prints 12