print [x for x in range(100,1000) if str(x)[0]==str(x)[2]] 是不是更精简
print [x for x in range(100,1000) if str(x)[0]==str(x)[2]]
print [x for x in range(100,1000) if str(x)[0]==str(x)[2]]
2015-03-25
没看出来有多精简,而且100遍历到1000得耗费多长时间~还不如:
print [x*100+y*10+x for x in range(1,9) for y in range(1,9)]
不过感觉还是以字符形式组合快,虽然占资源较多:(不知道对不对,凑合看吧)
import time, sys t1=time.time() a=[x for x in range(100,1000) if str(x)[0]==str(x)[2]] t2=time.time() print t2-t1 t1=time.time() b=[x+y+x for x in '123456789' for y in '0123456789'] t2=time.time() print t2-t1 c=[x*100+y*10+x for x in range(1,9) for y in range(1,9)] t2=time.time() print t2-t1 print sys.getsizeof(a) print len(a) print sys.getsizeof(b) print len(b) print sys.getsizeof(c) print len(c)
举报