我又要喷这本书了,按照标题和流程的意思,出题者是想让学生们这样操作的。
但是他的示例,并没有写是谁大,而是直接出结果。那就是我第一次写的流程,多么的简单。
a=7
b=12
c=56
if a<b:
a=b
if a<c:
a=c
print(a)
if a<b:
if b<c:
print("c最大:%i"%c)
else:
print("b最大:%i"%b)
elif a>=c:
print("a最大:%i"%a)
else:""
print("c最大:%i"%c)
话说,
if a<b:
a=b
if a<c:
a=c
比
max(a,b,c)
还快
0.08494806289672852
0.2848389148712158
max(...)
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
max是支持很多数据类型的,但只能在同类型间进行对比。
a=97
b=12
c=56
# if a<b:
# a=b
# if a<c:
# a=c
# print(a)
# if a<b:
# if b<c:
# print("c最大:%i"%c)
# else:
# print("b最大:%i"%b)
# elif a>=c:
# print("a最大:%i"%a)
# else:""
# print("c最大:%i"%c)
import time
ss=time.time()
for i in range(1000000):
if a<b:
a=b
if a<c:
a=c
print(time.time()-ss)
a=97
b=12
c=56
ss=time.time()
for i in range(1000000):
if a<b:
if b<c:
""
else:
""
elif a>=c:
""
else:""
print(time.time()-ss)
a=97
b=12
c=56
ss=time.time()
for i in range(1000000):
max(a,b,c)
print(time.time()-ss)
a最大:
0.06997013092041016
0.11292600631713867
0.25385427474975586
b最大:
0.07995295524597168
0.11393404006958008
0.2768521308898926
c最大:
0.06696057319641113
0.1139364242553711
0.2558629512786865
效率差不多三个数值无论谁最大,效率差不多。丫的我现在不想细扣效率了,时间都飞了!