Hướng dẫn how do you find the difference between two arrays in python? - làm thế nào để bạn tìm thấy sự khác biệt giữa hai mảng 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

Chúng ta có thể trừ hai mảng trong Python không?

Phương thức Numpy.SubTract () của Python trừ hai phần tử khôn ngoan. subtract() method subtracts two arrays element-wise.

Làm thế nào để bạn tìm thấy sự khác biệt giữa các mảng numpy?

Bước 1: Nhập Numpy.Step 2: Xác định hai mảng numpy.Bước 4: In đầu ra. Step 2: Define two numpy arrays. Step 3: Find the set difference between these arrays using the setdiff1d() function. Step 4: Print the output.

Làm cách nào để so sánh hai mảng có kích thước khác nhau python?

Pad mảng nhỏ hơn dài bằng mảng dài hơn, sau đó cơ sở một từ khác và nhìn với NP.Trong đó ((ARR1-ARR2) == 0).

Làm thế nào để bạn trừ hai mảng numpy trong Python?

Cách đơn giản nhất để trừ hai ma trận trong Numpy là bằng cách sử dụng toán tử, đó là sự đơn giản hóa của NP.Phương thức trừ () - Phương thức cụ thể không được thiết kế để trừ các mảng và các đối tượng giống như mảng khác như ma trận.using the - operator, which is the simplification of the np. subtract() method - NumPy specific method designed for subtracting arrays and other array-like objects such as matrices.