python中要怎样比较两个列表

这篇文章主要介绍python中要怎样比较两个列表,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

python中两个列表的比较

python中要怎样比较两个列表

思路:

首先判断列表是否等长;

如果等长,判断对应索引位置的值是否相同;

如果不同,记录两者的误差值和索引值

代码如下:

def compare(list1, list2):
    error = []
    error_index = []
    if len(list1) == len(list2):
        for i in range(0, len(list1)):
        #两个列表对应元素相同,则直接过
            if list1[i] == list2[i]:
                pass
            else:#两个列表对应元素不同,则输出对应的索引
                error.append(abs(list1[i]-list2[i]))
                # print(i)
                error_index.append(i)
    print(error)
    print(error_index)

以上是python中要怎样比较两个列表的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!