joe*_*ker 22
for x in thousand[400:500]:
pass
Run Code Online (Sandbox Code Playgroud)
如果您正在使用iterable而不是列表,则应使用itertools:
import itertools
for x in itertools.islice(thousand, 400, 500):
pass
Run Code Online (Sandbox Code Playgroud)
如果需要循环thousand[500],则使用501作为后一索引.即使thousand[501]不是有效索引,这也会起作用.
for element in allElements[400:501]:
# do something
Run Code Online (Sandbox Code Playgroud)
这些是切片并生成整个列表的子列表.它们是Python的主要元素之一.