内置函数range
range([start,] stop[, step])
第一个参数表示步长(默认为0),第二个参数表示终止值(结果中不包括这个值),第三个参数表示步长(默认为1, 实验发现不能用double)
列表元素的添加
import time
result = []
start = time.time()
for i in range(10000):
result = result + [i]
print(len(result), ",", time.time() - start)
result = []
start = time.time()
for i in range(10000):
result.append(i)
print(len(result), ",", time.time() - start)
运行结果
10000 , 0.2917604446411133
10000 , 0.000997304916381836
+
创建一个新的列表,并将原列表中的元素和新元素依次复制到新列表的内存空间。
append()
原地修改列表,是真正意义上的在列表尾部添加元素