玩转自动化运维全流程
检测列表中仅包含一种元素
检查一个列表能否仅包含一种元素,这里运用一点小技巧,也就是计算一个元素呈现个数,运用count能否和列表长度相同,假如相同,就阐明该列表中一切元素都是分歧的。
a = [3, 3, 3,3]
print("All element are duplicate in listOne", a.count(a[0]) == len(a))
b = [3, 3, 3,2]
print("All element are duplicate in listTwo", b.count(b[0]) == len(b))
复制代码
计算函数耗时
通常我们需求理解一下我们函数所耗费时间,来权衡函数的性能。
import time startTime = time.time()
# 你的代码
endTime = time.time()
totalTime = endTime - startTime
print("Total time required to execute code is= ", totalTime)