以下是一个示例代码,演示了如何遍历元组并将索引值与列表中的项进行比较:
my_tuple = ("apple", "banana", "orange", "grape")
my_list = ["apple", "banana", "banana", "grape"]
for index, item in enumerate(my_tuple):
if item == my_list[index]:
print(f"The item at index {index} is the same in both tuple and list.")
else:
print(f"The item at index {index} is different in tuple and list.")
输出结果为:
The item at index 0 is the same in both tuple and list.
The item at index 1 is the same in both tuple and list.
The item at index 2 is different in tuple and list.
The item at index 3 is the same in both tuple and list.