代码示例:
def match_lists(list1, tuple1):
if len(list1) != len(tuple1):
return False
for i in range(len(list1)):
if list1[i] != tuple1[i]:
return False
return True
list1 = [1, 2, 3]
tuple1 = (1, 2, 3)
tuple2 = (1, 2, 4)
print(match_lists(list1, tuple1)) # True
print(match_lists(list1, tuple2)) # False
解释:该函数接收两个参数list1和tuple1,分别表示待匹配的列表和元组。在函数内部,首先判断列表和元组的长度是否相等,如果不相等则直接返回False。接着运用for循环分别比较两个数据结构中的每个元素是否相等,如果有不相等的就返回False,否则返回True。最后根据函数返回值判断列表和元组是否匹配,将结果打印输出。