Python 使用循环查找列表中的最大数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14448692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find the maximum number in a list using a loop
提问by user1998529
So I have this list and variables:
所以我有这个列表和变量:
nums = [14, 8, 9, 16, 3, 11, 5]
big = nums[0]
spot = 0
I'm confused on how to actually do it. Please help! I'm new to Python and I want to use this exercise to give me a starter. I want to start with "repeat length of list" like in Scratch or BYOB, but how do I do that on Python?
我对如何真正做到这一点感到困惑。请帮忙!我是 Python 新手,我想用这个练习给我一个入门。我想从“重复列表长度”开始,就像在 Scratch 或 BYOB 中一样,但我如何在 Python 上做到这一点?
回答by alex
Here you go...
干得好...
nums = [14, 8, 9, 16, 3, 11, 5]
big = max(nums)
spot = nums.index(big)
This would be the Pythonic way of achieving this. If you want to use a loop, then loop with the current max value and check if each element is larger, and if so, assign to the current max.
这将是实现这一目标的 Pythonic 方式。如果要使用循环,则使用当前最大值循环并检查每个元素是否更大,如果是,则分配给当前最大值。
回答by Andrea
Why not simply using the built-in max()function:
为什么不简单地使用内置的max()函数:
>>> m = max(nums)
By the way, some answers to similar questions might be useful:
顺便说一句,类似问题的一些答案可能有用:
回答by helmbert
Usually, you could just use
通常,你可以使用
max(nums)
If you explicitly want to use a loop, try:
如果您明确想使用循环,请尝试:
max_value = None
for n in nums:
if n > max_value: max_value = n
回答by Emanuele Paolini
nums = [14, 8, 9, 16, 3, 11, 5]
big = None
spot = None
for i, v in enumerate(nums):
if big is None or v > big:
big = v
spot = i
回答by Rushy Panchal
To address your second question, you can use a forloop:
要解决您的第二个问题,您可以使用for循环:
for i in range(len(list)):
# do whatever
You should note that range()can have 3 arguments: start, end, and step. Start is what number to start with (if not supplied, it is 0); start is inclusive.. End is where to end at (this has to be give); end is exclusive: if you do range(100), it will give you 0-99. Step is also optional, it means what interval to use. If step is not provided, it will be 1. For example:
你应该注意到,range()可以有3个参数:start,end,和step。Start 是从哪个数字开始(如果未提供,则为 0);start 是包容性的.. End 是结束的地方(必须给出);end 是独占的:如果你这样做range(100),它会给你 0-99。step 也是可选的,它表示使用什么间隔。如果未提供步骤,则为 1。例如:
>>> x = range(10, 100, 5) # start at 10, end at 101, and use an interval of 5
>>> x
[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95] # note that it does not hit 100
Since endis exclusive, to include 100, we could do:
由于end是独占的,要包括 100,我们可以这样做:
>>> x = range(10, 101, 5) # start at 10, end at 101, and use an interval of 5
>>> x
[10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100] # note that it does hit 100
回答by Harun ERGUL
Python already has built in function for this kind of requirement.
Python 已经为这种需求内置了函数。
list = [3,8,2,9]
max_number = max(list)
print max_number # it will print 9 as big number
however if you find the max number with the classic vay you can use loops.
但是,如果您找到经典 vay 的最大数量,则可以使用循环。
list = [3,8,2,9]
current_max_number = list[0]
for number in list:
if number>current_max_number:
current_max_number = number
print current_max_number #it will display 9 as big number
回答by ERIC
scores = [12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27,
28, 29, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 31, 31, 37,
56, 75, 23, 565]
# initialize highest to zero
highest = 0
for mark in scores:
if highest < mark:
highest = mark
print(mark)
回答by Jayna
For the Max in List Code HS I've managed to get most of the auto grader to work for me using this code:
对于列表代码 HS 中的最大值,我使用以下代码设法让大部分自动分级机为我工作:
list = [-3,-8,-2,0]
current_max_number = list[0]
for number in list:
if number>current_max_number:
current_max_number = number
print current_max_number
def max_int_in_list():
print "Here"
I'm not sure where the max_int_in_list goes though. It needs to have exactly 1 parameter.
我不确定 max_int_in_list 在哪里。它需要正好有 1 个参数。
回答by Patrick Atwater
To print the Index of the largest number in a list.
打印列表中最大数字的索引。
numbers = [1,2,3,4,5,6,9]
N = 0
for num in range(len(numbers)) :
if numbers[num] > N :
N = numbers[num]
print(numbers.index(N))

