Python – 检查列表元素的元素索引是否相等
给定两个列表和检查列表,测试检查列表中的每个元素是否在两个列表中的相似索引中出现。
Input : test_list1 = [2, 6, 9, 7, 8], test_list2 = [2, 7, 9, 4, 8], check_list = [9, 8, 7]
Output : False
Explanation : 7 is at 4th and 2nd place in both list, hence False.
Input : test_list1 = [2, 6, 9, 7, 8], test_list2 = [2, 6, 9, 4, 8], check_list = [9, 8, 6]
Output : True
Explanation : All from checklist at similar index, hence True.
方法#1:使用循环
在此,我们迭代列表中的所有元素,如果元素不同并且存在于检查列表中,则返回 False。
Python3
# Python3 code to demonstrate working of
# Check if elements index are equal for list elements
# Using loop
# initializing lists
test_list1 = [2, 6, 9, 7, 8]
test_list2 = [2, 7, 9, 4, 8]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# initializing check_list
check_list = [9, 8, 2]
res = True
for idx, ele in enumerate(test_list1):
# check for indifference and containment
if test_list1[idx] != test_list2[idx] and ele in check_list:
res = False
break
# printing result
print("Are elements at same index for required instances ?: " + str(res))
Python3
# Python3 code to demonstrate working of
# Check if elements index are equal for list elements
# Using zip() + all() + generator expression
# initializing lists
test_list1 = [2, 6, 9, 7, 8]
test_list2 = [2, 7, 9, 4, 8]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# initializing check_list
check_list = [9, 8, 2]
# checking for all elements equal in check list using all()
res = all(a == b for a, b in zip(test_list1, test_list2) if a in check_list)
# printing result
print("Are elements at same index for required instances ?: " + str(res))
输出
The original list 1 : [2, 6, 9, 7, 8]
The original list 2 : [2, 7, 9, 4, 8]
Are elements at same index for required instances ?: True
方法 #2:使用 zip() + all() + 生成器表达式
在此,我们使用 zip() 对类似索引进行配对,然后使用 all() 检查所有索引,生成器表达式用于迭代逻辑。
Python3
# Python3 code to demonstrate working of
# Check if elements index are equal for list elements
# Using zip() + all() + generator expression
# initializing lists
test_list1 = [2, 6, 9, 7, 8]
test_list2 = [2, 7, 9, 4, 8]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# initializing check_list
check_list = [9, 8, 2]
# checking for all elements equal in check list using all()
res = all(a == b for a, b in zip(test_list1, test_list2) if a in check_list)
# printing result
print("Are elements at same index for required instances ?: " + str(res))
输出
The original list 1 : [2, 6, 9, 7, 8]
The original list 2 : [2, 7, 9, 4, 8]
Are elements at same index for required instances ?: True