Home  >  Article  >  Backend Development  >  How to compare two lists in python

How to compare two lists in python

(*-*)浩
(*-*)浩Original
2019-08-02 14:58:317071browse

Comparison of two lists in python

How to compare 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 two

The 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)
For more Python-related technical articles, please visit the

Python Tutorial column. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn