访问列表元素
通过索引切片访问
a = ["python","java","c++",["python","java","c++"]]
print(a[0[)
print(a[1:3])
print(a[:3])#取3之前的元素,不包括3
print(a[2:])#取二之后的元素,包括2
执行结果如下
通过for循环遍历
a = ["python","java","c++",["python","java","c++"]]
for item in a:
print(item)
结果如下
当需要每个元素的索引值时也可以使用enumerate函数
例如
a = ["python","java","c++",["python","java","c++"]]
for index,item in enumerate(a):
print(index,item)
结果如下