那max()
怎么样呢?
highest = max(1, 2, 3) # or max([1, 2, 3]) for lists
这种方法不使用
max()
函数
a = [1,2,3,4,6,7,99,88,999]
max_num = 0
for i in a:
if i > max_num:
max_num = i
print(max_num)
如果你想找到结果最大值的索引,
print(a.index(max_num))
使用 max() 函数的直接方法
max()
函数返回具有最高值的项,或者在可迭代对象中具有最高值的项。
例如:当您需要在整数/数字上查找最大值时
a = (1, 5, 3, 9)
print(max(a))
>> 9
例子:当你有一个字符串时
x = max("Mike", "John", "Vicky")
print(x)
>> Vicky
它基本上返回按字母顺序排序的最高值的名称。
max
,因为它是内置函数的名称。 - wjandrea使用max()
>>> l = [1, 2, 5]
>>> max(l)
5
>>>
sorted(l,reverse=True)
l = [1, 2, 3]
sort=sorted(l,reverse=True)
print(sort)
您将获得:
[3,2,1]
但是如果想要获得最大值,可以这样做:
print(sort[0])
您将获得:
3
如果存在第二大的值:
print(sort[1])
and so on...
max
是 Python 中的内置函数,用于从序列(如列表、元组、集合等)中获取最大值。
print(max([9, 7, 12, 5]))
# prints 12
#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)