有没有办法从列表中获取前10个结果。可能是这样的:
list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
list.fetch(10)
发布于 2012-06-05 20:32:30
看看这个
list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
list[0:10]
输出:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
发布于 2012-06-05 20:34:40
itertools模块中有很多很棒的东西。因此,如果标准切片(由Levon使用)不能执行您想要的操作,那么可以尝试使用islice
函数:
from itertools import islice
l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
iterator = islice(l, 10)
for item in iterator:
print item
发布于 2012-06-05 20:32:18
使用切片运算符:
list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
list[:10]
https://stackoverflow.com/questions/10897339
复制相似问题