Comparison of two lists in python
# Ideas:(Recommended learning:Python video tutorial)
First determine whether the lists are of equal length; If they are equal, determine whether the values at the corresponding index positions are the same; If they are different, Record the error value and index value of the twoThe code is as follows:
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 Tutorialcolumn. study!
The above is the detailed content of How to compare two lists in python. For more information, please follow other related articles on the PHP Chinese website!