python怎样用最简单的for loop求list中的最大值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python怎样用最简单的for loop求list中的最大值相关的知识,希望对你有一定的参考价值。

小弟刚学到for loop,被这个问题难倒了
不要用max之类的,,只用for loop和if
设定一个现有值为最大,如果碰到更大的值就替换现有值,然后print("biggest number:" ,biggestnum)

参考技术A one_long_list=[13,34,3,343,343,3434324]
biggestnum=one_long_list[0]
for i in one_long_list[1:]:
   if(biggestnum<i):
      biggestnum=i
print("biggest number: ",biggestnum)

本回答被提问者和网友采纳

Python06:for循环

初识for循环:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

for i in range(10):
    print ("loop", i)

 

输出:

loop 0

loop 1

loop 2

loop 3

loop 4

loop 5

loop 6

loop 7

loop 8

loop 9

 

Process finished with exit code 0

解释:

range(10)就相当于产生10个数字(0123456789

 

猜数字用for循环实现:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

age = 56

for i in range(3):
    guess_age = int(input("guess age:"))
    if guess_age == age:
        print("yes, you got it.")
        break
    elif guess_age > age:
        print("think smaller...")
    else:
        print("think bigger...")
else:
    print("you have tried too many times... fuck off")

 

结果略,同while产生同样的效果,也可以接else,同while一样。

for循环打印(设置步长):

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

for i in range(0,10,1):
    print("lo

 

输出结果:

loop 0

loop 1

loop 2

loop 3

loop 4

loop 5

loop 6

loop 7

loop 8

loop 9

 

Process finished with exit code 0

 

for循环打印(设置步长):

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

for i in range(0,10,2):
    print("loop", i)

 

输出结果:

loop 0

loop 2

loop 4

loop 6

loop 8

 

Process finished with exit code 0

 

for循环打印(设置步长):

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

for i in range(0,10,3):
    print("loop", i)

 

输出结果:

loop 0

loop 3

loop 6

loop 9

 

Process finished with exit code 0

解释:

rangex,y,z):x起始值;

             y:终止值;

             z:步长。结合程序理解。



























以上是关于python怎样用最简单的for loop求list中的最大值的主要内容,如果未能解决你的问题,请参考以下文章

python中的for循环取list中的第i 个值值

python请问for循环嵌套执行顺序是怎样的?求大神指点!

poj1631——树状数组求LIS

Python06:for循环

C++中,do while, for loop和while loop之间有啥不同

python怎样实现arduino那样的运行结构setup()--loop()?