试试看:
x = list[0:8]
print(x)
结果:
Traceback (most recent call last):
File "script.py", line 1, in <module>
x = list[0:8]
TypeError: 'type' object is not subscriptable
这是错误的,因为它不起作用。你用
[x:x]
从列表中选择项目片段,而不是生成列表。你可以使用范围,或者使用列表理解。
x = list(x for x in range(0, 100))
print(f"There are {len(x)} items in the list")
结果
"There are 100 items in the list"
把它切成片,最后50个项目,
[50:]
表示我们从第50个索引开始,在
:
意思是一直走到最后。相反地你可以做
[:80]
它会从一开始上升到第80个指数,这和
[0:80]
.
half = x[50:]
print(f"There are {len(half )} items in the list")
结果
"There are 50 items in the list"