Hướng dẫn how do you find the difference between two lists in python? - làm thế nào để bạn tìm thấy sự khác biệt giữa hai danh sách trong python?

Trong trường hợp bạn muốn sự khác biệt đệ quy, tôi đã viết một gói cho Python: https://github.com/sperman/deepdiff

Cài đặt

Cài đặt từ Pypi:

pip install deepdiff

Ví dụ sử dụng

Nhập khẩu

>>> from deepdiff import DeepDiff
>>> from pprint import pprint
>>> from __future__ import print_function # In case running on Python 2

Cùng một đối tượng trả về trống

>>> t1 = {1:1, 2:2, 3:3}
>>> t2 = t1
>>> print(DeepDiff(t1, t2))
{}

Loại vật phẩm đã thay đổi

>>> t1 = {1:1, 2:2, 3:3}
>>> t2 = {1:1, 2:"2", 3:3}
>>> pprint(DeepDiff(t1, t2), indent=2)
{ 'type_changes': { 'root[2]': { 'newtype': ,
                                 'newvalue': '2',
                                 'oldtype': ,
                                 'oldvalue': 2}}}

Giá trị của một mặt hàng đã thay đổi

>>> t1 = {1:1, 2:2, 3:3}
>>> t2 = {1:1, 2:4, 3:3}
>>> pprint(DeepDiff(t1, t2), indent=2)
{'values_changed': {'root[2]': {'newvalue': 4, 'oldvalue': 2}}}

Mục được thêm và/hoặc bị xóa

>>> t1 = {1:1, 2:2, 3:3, 4:4}
>>> t2 = {1:1, 2:4, 3:3, 5:5, 6:6}
>>> ddiff = DeepDiff(t1, t2)
>>> pprint (ddiff)
{'dic_item_added': ['root[5]', 'root[6]'],
 'dic_item_removed': ['root[4]'],
 'values_changed': {'root[2]': {'newvalue': 4, 'oldvalue': 2}}}

Chuỗi chênh lệch

>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":"world"}}
>>> t2 = {1:1, 2:4, 3:3, 4:{"a":"hello", "b":"world!"}}
>>> ddiff = DeepDiff(t1, t2)
>>> pprint (ddiff, indent = 2)
{ 'values_changed': { 'root[2]': {'newvalue': 4, 'oldvalue': 2},
                      "root[4]['b']": { 'newvalue': 'world!',
                                        'oldvalue': 'world'}}}

Chuỗi chênh lệch 2

>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":"world!\nGoodbye!\n1\n2\nEnd"}}
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":"world\n1\n2\nEnd"}}
>>> ddiff = DeepDiff(t1, t2)
>>> pprint (ddiff, indent = 2)
{ 'values_changed': { "root[4]['b']": { 'diff': '--- \n'
                                                '+++ \n'
                                                '@@ -1,5 +1,4 @@\n'
                                                '-world!\n'
                                                '-Goodbye!\n'
                                                '+world\n'
                                                ' 1\n'
                                                ' 2\n'
                                                ' End',
                                        'newvalue': 'world\n1\n2\nEnd',
                                        'oldvalue': 'world!\n'
                                                    'Goodbye!\n'
                                                    '1\n'
                                                    '2\n'
                                                    'End'}}}

>>> 
>>> print (ddiff['values_changed']["root[4]['b']"]["diff"])
--- 
+++ 
@@ -1,5 +1,4 @@
-world!
-Goodbye!
+world
 1
 2
 End

Loại thay đổi

>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3]}}
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":"world\n\n\nEnd"}}
>>> ddiff = DeepDiff(t1, t2)
>>> pprint (ddiff, indent = 2)
{ 'type_changes': { "root[4]['b']": { 'newtype': ,
                                      'newvalue': 'world\n\n\nEnd',
                                      'oldtype': ,
                                      'oldvalue': [1, 2, 3]}}}

Danh sách sự khác biệt

>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3, 4]}}
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2]}}
>>> ddiff = DeepDiff(t1, t2)
>>> pprint (ddiff, indent = 2)
{'iterable_item_removed': {"root[4]['b'][2]": 3, "root[4]['b'][3]": 4}}

Danh sách chênh lệch 2:

>>> from deepdiff import DeepDiff
>>> from pprint import pprint
>>> from __future__ import print_function # In case running on Python 2
0

Danh sách sự khác biệt bỏ qua thứ tự hoặc sao chép: (với cùng một từ điển như trên)

>>> from deepdiff import DeepDiff
>>> from pprint import pprint
>>> from __future__ import print_function # In case running on Python 2
1

Danh sách có chứa từ điển:

>>> from deepdiff import DeepDiff
>>> from pprint import pprint
>>> from __future__ import print_function # In case running on Python 2
2

Sets:

>>> from deepdiff import DeepDiff
>>> from pprint import pprint
>>> from __future__ import print_function # In case running on Python 2
3

Có tên là Tuples:

>>> from deepdiff import DeepDiff
>>> from pprint import pprint
>>> from __future__ import print_function # In case running on Python 2
4

Đối tượng tùy chỉnh:

>>> from deepdiff import DeepDiff
>>> from pprint import pprint
>>> from __future__ import print_function # In case running on Python 2
5

Thuộc tính đối tượng được thêm vào:

>>> from deepdiff import DeepDiff
>>> from pprint import pprint
>>> from __future__ import print_function # In case running on Python 2
6

Làm thế nào để tôi tìm thấy sự khác biệt giữa hai danh sách trong Python?

Phương pháp 6: Sử dụng symmetric_difference để tìm sự khác biệt giữa hai danh sách trong Python.Các phần tử trong tập đầu tiên hoặc bộ thứ hai được trả về bằng kỹ thuật symmetric_difference ().Giao lộ, không giống như các mục được chia sẻ của hai bộ, không được trả lại bởi kỹ thuật này.Use symmetric_difference to Find the Difference Between Two Lists in Python. The elements that are either in the first set or the second set are returned using the symmetric_difference() technique. The intersection, unlike the shared items of the two sets, is not returned by this technique.

Làm thế nào để bạn so sánh hai danh sách khác nhau?

So sánh hai danh sách trong Excel..
Phương pháp 1: So sánh hai danh sách bằng toán tử dấu hiệu bằng nhau ..
Phương pháp 2: Kết hợp dữ liệu bằng cách sử dụng kỹ thuật khác biệt hàng ..
Phương pháp 3: Kết hợp chênh lệch hàng bằng cách sử dụng nếu điều kiện ..
Phương pháp 4: Kết hợp dữ liệu ngay cả khi có sự khác biệt hàng ..
Phương pháp 5: Đánh dấu tất cả các dữ liệu phù hợp bằng cách sử dụng định dạng có điều kiện ..

Bạn có thể trừ hai danh sách trong Python không?

Sử dụng Numpy để trừ hai Python liệt kê một trong các phương thức mà Numpy cung cấp là phương thức trừ ().Phương thức lấy hai mảng s numpy làm đầu vào và cung cấp các phép trừ yếu tố giữa hai danh sách. One of the methods that numpy provides is the subtract() method. The method takes two numpy array s as input and provides element-wise subtractions between the two lists.