python中两个列表的比较
思路:(推荐学习:Python视频教程)
首先判断列表是否等长;
如果等长,判断对应索引位置的值是否相同;
如果不同,记录两者的误差值和索引值
代码如下:
复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
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如何比较两个列表的全部内容,更多相关python如何比较两个列表内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复