要比较嵌套列表中的两个项,需要先确定这两个项的位置。假设我们有一个包含嵌套列表的列表:
my_list = [[1, 2], [3, 4], [5, 6]]
我们可以使用以下代码比较第二个嵌套列表中下标为0的项和第三个嵌套列表中下标为1的项:
if my_list[1][0] == my_list[2][1]:
print("The items are the same.")
else:
print("The items are not the same.")
这段代码会输出"The items are not the same.",因为my_list[1][0]的值为3,my_list[2][1]的值为6。您可以更改这两个项的值来查看代码是否按预期工作。
上一篇:比较嵌套列表中包含的所有列表,以获取仅匹配的字符串。
下一篇:比较嵌套列表中的值