下面是一个示例代码,演示了如何遍历一个具有固定索引的元组列表:
tuple_list = [(1, 2), (3, 4), (5, 6)]
for i in range(len(tuple_list)):
tuple_item = tuple_list[i]
print(f"Tuple at index {i}: {tuple_item}")
for j in range(len(tuple_item)):
print(f"Element at index {j}: {tuple_item[j]}")
print()
输出结果如下:
Tuple at index 0: (1, 2)
Element at index 0: 1
Element at index 1: 2
Tuple at index 1: (3, 4)
Element at index 0: 3
Element at index 1: 4
Tuple at index 2: (5, 6)
Element at index 0: 5
Element at index 1: 6
在这个示例中,我们首先使用range(len(tuple_list))
来遍历元组列表的索引。然后,我们使用tuple_list[i]
来访问每个元组。在每个元组中,我们再次使用range(len(tuple_item))
来遍历元组中的元素索引,并使用tuple_item[j]
来访问每个元素。
上一篇:遍历一个句柄以获取内部元素的文本
下一篇:遍历一个开放图层要素对象数组