import time
start_time = time.time()
for a in range(0,1001):
for b in range(0,1001):
for c in range(0,1001):
if a+b+c == 1000 and a**2 +b**2 == c**2:
print("a,b,c:%d, %d, %d"%(a,b,c))
end_time = time.time()
print("times:%d" % (end_time-start_time))
print("finished")
import time
start_time = time.time()
for a in range(0,1001):
for b in range(0,1001):
c = 1000 - a - b
if a**2 ++b**2 ==c**2:
print("a,b,c:%d, %d, %d"%(a,b,c))
end_time = time.time()
print("times:%d" % (end_time-start_time))
print("finished")
from timeit import Timer
# li1 = [1,2]
# li2 = [23,5]
# li = li1 +li2 #加操作
# li = [i for i in range(10000)] #列表生成器
# li = list(range(10000)) #把可迭代的对象直接转化成列表
def t1():
li = [] #在空列表中进行追加
for i in range(10000):
li.append(i)
def t2():
li = []
for i in range(10000):
li += [i]
def t3():
li = [i for i in range(10000)]
def t4():
li = list(range(10000))
timer1 = Timer("t1()","from __main__ import t1")
print("append:",timer1.timeit(1000))
timer2 = Timer("t2()","from __main__ import t2")
print("+:",timer2.timeit(1000))
timer3 = Timer("t3()","from __main__ import t3")
print("[i for i in range]:",timer3.timeit(1000))
timer4 = Timer("t4()","from __main__ import t4")
print("list(range()):",timer4.timeit(1000))
def t6():
li = []
for i in range(10000):
li.append(i) #在队尾追加
def t7():
li = []
for i in range(10000):
li.insert(0,i) #在队头追加
timer6 = Timer("t6()","from __main__ import t6")
print("list(range()):",timer6.timeit(1000))
timer7 = Timer("t7()","from __main__ import t7")
print("list(range()):",timer7.timeit(1000))
依次操作:取值,赋值,尾部追加,尾部弹出值,指定位置弹出值,指定位置插入,删除整个列表,迭代,s使用in操作符判断参数是否在这个列表中,取切片(只和距离有关系),删除切片,设置切片,逆置列表,两个列表加在一起,排序,列表的每一个元素乘以一个固定实数。
依次操作:复制字典表,取值,设置值,删除,检查值是否存在(字典不需要遍历),迭代
解决的是数据如何保存的问题
注:定义出数据类型支持的操作接口。