Python - 检查特定索引中的元素是否与列表元素相等
当需要检查特定索引中的元素是否等于另一个元素列表时,使用简单迭代和布尔值。
示例
以下是相同的演示-
my_list_1 = [69, 96, 23, 57, 13, 75, 13]
my_list_2 = [68, 21, 69, 23, 49, 35, 73]
print("第一个列表是: " )
print(my_list_1)
print("排序后的第一个列表是:")
my_list_1.sort()
print(my_list_1)
print("第二个名单是: " )
print(my_list_2)
print("排序后的第一个列表是:")
my_list_2.sort()
print(my_list_2)
check_list = [66, 89, 69]
print("第二个名单是: " )
print(check_list)
print("排序后的检查清单是:")
check_list.sort()
print(check_list)
my_result = True
for index, element in enumerate(my_list_1):
if my_list_1[index] != my_list_2[index] and element in check_list:
my_result = False
break
if(my_result == True):
print("The elements of the list are equal to the elements in the check list")
else:
print("The elements of the list aren't equal to elements in the check list")输出结果第一个列表是: [69, 96, 23, 57, 13, 75, 13] 排序后的第一个列表是: [13, 13, 23, 57, 69, 75, 96] 第二个名单是: [68, 21, 69, 23, 49, 35, 73] 排序后的第一个列表是: [21, 23, 35, 49, 68, 69, 73] 第二个名单是: [66, 89, 69] 排序后的检查清单是: [66, 69, 89] The elements of the list aren't equal to elements in the check list
解释
定义了两个整数列表并显示在控制台上。
它们被排序并显示在控制台上。
布尔值分配给True。
使用“枚举”迭代第一个列表。
比较特定索引处的元素,并检查该元素是否在第三个列表中找到。
如果未找到,则将布尔值分配给“False”。
控制跳出循环。
根据布尔值,消息显示在控制台上。
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短