Python中两个列表的比较

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29027865/article/details/83819447

python中两个列表的比较

思路:

  1. 首先判断列表是否等长;
  2. 如果等长,判断对应索引位置的值是否相同;
  3. 如果不同,记录两者的误差值和索引值
    代码如下:
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)

猜你喜欢

转载自blog.csdn.net/qq_29027865/article/details/83819447