查找数字列表中最大(最大、最大)的数字

发布于 2024-09-06 04:52:19 字数 161 浏览 8 评论 0原文

如何轻松找到给定数字列表中的最大数字?


另请参阅如何找到 2 个数字中的最大值(更大、更大)? - 在这种特殊情况下,两个数字也可以直接比较值。

How can I easily find the greatest number in a given list of numbers?


See also How do I find the maximum (larger, greater) of 2 numbers? - in that special case, the two values can also be compared directly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

初心未许 2024-09-13 04:52:19

怎么样 max()

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

What about max()

highest = max(1, 2, 3)  # or max([1, 2, 3]) for lists
不喜欢何必死缠烂打 2024-09-13 04:52:19

您可以使用带有多个参数的内置函数 max()

print max(1, 2, 3)

或列表:

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

或实际上任何可迭代的东西。

You can use the inbuilt function max() with multiple arguments:

print max(1, 2, 3)

or a list:

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

or in fact anything iterable.

柠檬色的秋千 2024-09-13 04:52:19

此方法不使用 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

它基本上返回具有最高值的名称,按字母顺序排列。

This approach is without using max() function

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)

Also if you want to find the index of the resulting max,

print(a.index(max_num))

Direct approach by using function max()

max() function returns the item with the highest value, or the item with the highest value in an iterable

Example: when you have to find max on integers/numbers

a = (1, 5, 3, 9)
print(max(a))
>> 9

Example: when you have string

x = max("Mike", "John", "Vicky")
print(x)
>> Vicky

It basically returns the name with the highest value, ordered alphabetically.

多孤肩上扛 2024-09-13 04:52:19

使用 max()

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

Use max()

>>> l = [1, 2, 5]
>>> max(l)
5
>>> 
对你的占有欲 2024-09-13 04:52:19

max 是Python中的内置函数,用于从序列中获取最大值,即(列表、元组、集合等)

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

# prints 12 

max is a builtin function in python, which is used to get max value from a sequence, i.e (list, tuple, set, etc..)

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

# prints 12 
无远思近则忧 2024-09-13 04:52:19

你实际上可以对它进行排序:

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])

依此类推......

You can actually sort it:

sorted(l,reverse=True)

l = [1, 2, 3]
sort=sorted(l,reverse=True)
print(sort)

You get:

[3,2,1]

But still if want to get the max do:

print(sort[0])

You get:

3

if second max:

print(sort[1])

and so on...

靖瑶 2024-09-13 04:52:19
    #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)
    #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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文