So sánh danh sách hoạt động như thế nào trong Python

Kiểm tra xem hai danh sách có bằng nhau không, phần tử nào khớp với nhau, nhận sự khác biệt giữa hai danh sách, so sánh danh sách từ điển, danh sách chuỗi, v.v.

So sánh danh sách hoạt động như thế nào trong Python

Show

Miguel Brito

·Ngày 12 tháng 12 năm 2021·17 phút đọc

Theo dõi bản tin của tôi và không bao giờ bỏ lỡ các bài viết sắp tới của tôi

Đặt mua

Chơi bài viết này

Trình duyệt của bạn không hỗ trợ phần tử âm thanh. TỐC ĐỘ1X

Mục lục

Cách đây một thời gian, tôi đã viết một hướng dẫn về cách so sánh hai từ điển trong Python 3 và nhiệm vụ này không đơn giản như nó có vẻ như thế nào. Hóa ra so sánh hai danh sách trong Python cũng khó như so sánh các

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
3

Cách chúng ta được dạy để so sánh hai đối tượng trong Python có một chút sai lệch. Hầu hết các sách và hướng dẫn dạy so sánh đối tượng bằng cách sử dụng toán tử

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4 hoặc
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
5. Trên thực tế, hai toán tử này chỉ chiếm một phần nhỏ trong các trường hợp sử dụng thường xuyên nhất

Ví dụ

  • điều gì sẽ xảy ra nếu chúng ta muốn so sánh một danh sách các số dấu phẩy động có xét đến một dung sai nhất định?
  • điều gì sẽ xảy ra nếu chúng ta muốn đối chiếu hai danh sách nhưng bỏ qua thứ tự xuất hiện của các phần tử?
  • có lẽ chúng ta cần so sánh hai danh sách và trả về các phần tử giao nhau với cả hai
  • đôi khi chúng ta có thể muốn nhận được sự khác biệt giữa hai danh sách
  • Nếu chúng ta có hai danh sách chuỗi và cần so sánh chúng bằng cách bỏ qua các trường hợp chuỗi thì sao?
  • điều gì sẽ xảy ra nếu chúng tôi đưa ra một danh sách các mảng
    In [1]: import numpy as np
    
    In [2]: numbers = [np.ones(3), np.zeros(2)]
    
    In [3]: numbers
    Out[3]: [array([1., 1., 1.]), array([0., 0.])]
    
    In [4]: target = [np.ones(3), np.zeros(2)]
    
    In [5]: numbers == target
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-5-b832db4b039d> in <module>
    ----> 1 numbers == target
    
    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    
    6 để so sánh với nhau, chúng tôi có thể làm gì?
  • hoặc có thể chúng tôi có danh sách đối tượng tùy chỉnh hoặc danh sách từ điển

Danh sách này cứ lặp đi lặp lại và đối với tất cả các trường hợp sử dụng này, việc sử dụng

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4 không giúp được gì

Đó là những gì chúng ta sẽ thấy trong bài viết này. Chúng ta sẽ tìm hiểu những cách tốt nhất để so sánh hai danh sách trong Python cho một số trường hợp sử dụng khi toán tử

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4 là không đủ

Sẳn sàng?

So sánh nếu hai danh sách bằng nhau trong python

So sánh danh sách hoạt động như thế nào trong Python

Cách dễ nhất để so sánh hai danh sách bằng nhau là sử dụng toán tử

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4. Phương pháp so sánh này hoạt động tốt đối với các trường hợp đơn giản, nhưng như chúng ta sẽ thấy sau, nó không hoạt động với các so sánh nâng cao

Một ví dụ về trường hợp đơn giản sẽ là danh sách các đối tượng

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
0 hoặc
>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
1

>>> numbers = [1, 2, 3]
>>> target = [1, 2, 3]
>>> numbers == target
True
>>> [1, 2, 3] == [1, 3, 2]
False
>>> ['name', 'lastname'] == ['name', 'lastname']
True
>>> ['name', 'lastname'] == ['name', 'last name']   
False

Khá đơn giản, phải không? . Trong thế giới thực, mọi thứ trở nên phức tạp rất nhanh. Để minh họa, hãy xem xét các trường hợp sau

Giả sử bạn có một danh sách các dấu phẩy động được tạo động. Bạn có thể thêm các phần tử đơn lẻ hoặc các phần tử bắt nguồn từ một phép toán chẳng hạn như

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
2

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]

Rõ ràng, số học dấu phẩy động có những hạn chế của nó và đôi khi chúng ta muốn so sánh hai danh sách nhưng bỏ qua các lỗi chính xác hoặc thậm chí xác định một số dung sai. Đối với những trường hợp như thế này, toán tử

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4 sẽ không đủ

Mọi thứ có thể trở nên phức tạp hơn nếu danh sách có đối tượng tùy chỉnh hoặc đối tượng từ các thư viện khác, chẳng hạn như

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
6

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Bạn cũng có thể muốn so sánh các danh sách và trả lại kết quả khớp. Hoặc có thể so sánh hai danh sách và trả về sự khác biệt. Hoặc có lẽ bạn muốn so sánh hai danh sách bỏ qua các danh sách trùng lặp hoặc so sánh danh sách từ điển trong Python

Trong mọi trường hợp, sử dụng

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4 không phải là câu trả lời và đó là điều chúng ta sẽ thấy tiếp theo. cách thực hiện các thao tác so sánh phức tạp giữa hai danh sách trong Python

So sánh hai danh sách số float

Trong phần trước, chúng ta đã thấy rằng số học dấu phẩy động có thể gây ra lỗi chính xác. Nếu chúng ta có một danh sách các float và muốn so sánh nó với một danh sách khác, rất có thể toán tử

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4 sẽ không giúp được gì

Hãy xem lại ví dụ từ phần trước và xem cách tốt nhất để so sánh hai danh sách float là gì

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]

Như bạn thấy,

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
7, khiến phép so sánh không thành công. Bây giờ, làm thế nào chúng ta có thể làm tốt hơn?

Có một số cách để tiếp cận nhiệm vụ này. Một là tạo chức năng tùy chỉnh của riêng chúng ta, chức năng này lặp lại các phần tử và so sánh từng phần tử một bằng cách sử dụng hàm

May mắn thay, chúng ta không phải phát minh lại bánh xe. Như tôi đã trình bày trong bài viết "cách so sánh hai ký tự", chúng ta có thể sử dụng thư viện có tên là

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
9 để làm điều đó. Thư viện này hỗ trợ các loại đối tượng khác nhau và danh sách là một trong số đó

Ví dụ dưới đây bắt đầu bằng cách thiết lập hai danh sách mà chúng tôi muốn so sánh. Sau đó, chúng tôi chuyển nó tới hàm tạo

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
00 để trả về sự khác biệt. Thật tuyệt, giá trị trả về có nhiều thông tin hơn một giá trị boolean đơn giản

Vì chúng tôi muốn bỏ qua lỗi chính xác, chúng tôi có thể đặt số chữ số SAU dấu thập phân được sử dụng trong so sánh

Kết quả là một lệnh trống, có nghĩa là các danh sách bằng nhau. Nếu chúng ta thử so sánh một danh sách với một số float khác nhau hơn 3 chữ số có nghĩa, thư viện sẽ trả về sự khác biệt đó

Để tái tạo, trong bài viết này tôi đã sử dụng phiên bản mới nhất của

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
9 là
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
02

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
0

So sánh nếu hai danh sách không có thứ tự (danh sách không có thứ tự) bằng nhau

Các danh sách trong Python không được sắp xếp theo mặc định. Đôi khi chúng ta muốn so sánh hai danh sách nhưng coi chúng như nhau miễn là chúng có các phần tử giống nhau—bất kể thứ tự của chúng là gì

Có hai cách để làm điều này

  • sắp xếp danh sách và sử dụng toán tử
    In [1]: import numpy as np
    
    In [2]: numbers = [np.ones(3), np.zeros(2)]
    
    In [3]: numbers
    Out[3]: [array([1., 1., 1.]), array([0., 0.])]
    
    In [4]: target = [np.ones(3), np.zeros(2)]
    
    In [5]: numbers == target
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-5-b832db4b039d> in <module>
    ----> 1 numbers == target
    
    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    
    4
  • chuyển đổi chúng thành
    In [1]: import numpy as np
    
    In [2]: numbers = [np.ones(3), np.zeros(2)]
    
    In [3]: numbers
    Out[3]: [array([1., 1., 1.]), array([0., 0.])]
    
    In [4]: target = [np.ones(3), np.zeros(2)]
    
    In [5]: numbers == target
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-5-b832db4b039d> in <module>
    ----> 1 numbers == target
    
    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    
    04 và sử dụng toán tử
    In [1]: import numpy as np
    
    In [2]: numbers = [np.ones(3), np.zeros(2)]
    
    In [3]: numbers
    Out[3]: [array([1., 1., 1.]), array([0., 0.])]
    
    In [4]: target = [np.ones(3), np.zeros(2)]
    
    In [5]: numbers == target
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-5-b832db4b039d> in <module>
    ----> 1 numbers == target
    
    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    
    4
  • sử dụng
    >>> numbers = []
    >>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
    >>> numbers.append(0.2) # add a single element
    >>> target = [0.3, 0.2]
    >>> numbers == target  # compares the lists
    False
    >>> numbers  # Ooopppssss....
    [0.30000000000000004, 0.2]
    >>> target
    [0.3, 0.2]
    
    9

Hai phương thức đầu tiên này giả định rằng các phần tử có thể được so sánh một cách an toàn bằng cách sử dụng toán tử

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4. Cách tiếp cận này không hoạt động đối với các số có dấu phẩy động và các đối tượng phức tạp khác, nhưng như chúng ta đã thấy trong phần trước, chúng ta có thể sử dụng
>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
9

Sắp xếp danh sách và sử dụng toán tử In [1]: import numpy as np In [2]: numbers = [np.ones(3), np.zeros(2)] In [3]: numbers Out[3]: [array([1., 1., 1.]), array([0., 0.])] In [4]: target = [np.ones(3), np.zeros(2)] In [5]: numbers == target --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-5-b832db4b039d> in <module> ----> 1 numbers == target ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 4

So sánh danh sách hoạt động như thế nào trong Python

Bạn có thể sắp xếp danh sách trong Python theo hai cách khác nhau

  • sử dụng phương pháp
    In [1]: import numpy as np
    
    In [2]: numbers = [np.ones(3), np.zeros(2)]
    
    In [3]: numbers
    Out[3]: [array([1., 1., 1.]), array([0., 0.])]
    
    In [4]: target = [np.ones(3), np.zeros(2)]
    
    In [5]: numbers == target
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-5-b832db4b039d> in <module>
    ----> 1 numbers == target
    
    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    
    10
  • sử dụng chức năng
    In [1]: import numpy as np
    
    In [2]: numbers = [np.ones(3), np.zeros(2)]
    
    In [3]: numbers
    Out[3]: [array([1., 1., 1.]), array([0., 0.])]
    
    In [4]: target = [np.ones(3), np.zeros(2)]
    
    In [5]: numbers == target
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-5-b832db4b039d> in <module>
    ----> 1 numbers == target
    
    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    
    11

Phương pháp đầu tiên sắp xếp danh sách tại chỗ và điều đó có nghĩa là danh sách của bạn sẽ được sửa đổi. Bạn không nên sửa đổi danh sách tại chỗ vì nó có thể gây ra các lỗi khó phát hiện

Sử dụng

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
12 sẽ tốt hơn vì nó trả về một danh sách mới và giữ nguyên danh sách ban đầu

Hãy xem nó hoạt động như thế nào

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
1

Do đó, bằng cách sắp xếp các danh sách trước, chúng tôi đảm bảo rằng cả hai danh sách sẽ có cùng thứ tự và do đó có thể được so sánh bằng cách sử dụng toán tử

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4

Chuyển đổi các In [1]: import numpy as np In [2]: numbers = [np.ones(3), np.zeros(2)] In [3]: numbers Out[3]: [array([1., 1., 1.]), array([0., 0.])] In [4]: target = [np.ones(3), np.zeros(2)] In [5]: numbers == target --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-5-b832db4b039d> in <module> ----> 1 numbers == target ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 14 thành một In [1]: import numpy as np In [2]: numbers = [np.ones(3), np.zeros(2)] In [3]: numbers Out[3]: [array([1., 1., 1.]), array([0., 0.])] In [4]: target = [np.ones(3), np.zeros(2)] In [5]: numbers == target --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-5-b832db4b039d> in <module> ----> 1 numbers == target ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 04

So sánh danh sách hoạt động như thế nào trong Python

Trái ngược với danh sách, bộ trong Python không quan tâm đến thứ tự. Ví dụ: bộ

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
16 giống với bộ
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
17. Như vậy, chúng ta có thể sử dụng tính năng này để so sánh hai danh sách bỏ qua thứ tự của các phần tử

Để làm như vậy, chúng tôi chuyển đổi từng danh sách thành một tập hợp, sau đó sử dụng

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4 để so sánh chúng

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
1

Sử dụng thư viện >>> numbers = [] >>> numbers.append(0.1 + 0.1 + 0.1) # derive the element based on a summation >>> numbers.append(0.2) # add a single element >>> target = [0.3, 0.2] >>> numbers == target # compares the lists False >>> numbers # Ooopppssss.... [0.30000000000000004, 0.2] >>> target [0.3, 0.2] 9

Thư viện này cũng cho phép chúng tôi bỏ qua thứ tự theo thứ tự, chẳng hạn như

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
14s. Theo mặc định, nó sẽ xem xét thứ tự, nhưng nếu chúng tôi đặt
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
11 thành
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
12, thì tất cả chúng tôi đều ổn. Hãy xem điều này trong hành động

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
6

Sử dụng

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
9 có ưu và nhược điểm. Cuối cùng, đó là một thư viện bên ngoài mà bạn cần cài đặt, vì vậy nếu bạn có thể sử dụng
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
04 để so sánh các danh sách, thì hãy sử dụng nó. Tuy nhiên, nếu bạn có các trường hợp sử dụng khác mà nó có thể tỏa sáng, thì tôi sẽ sử dụng nó

Cách so sánh hai danh sách và trả về kết quả khớp

So sánh danh sách hoạt động như thế nào trong Python

Trong phần này, chúng ta sẽ xem cách so sánh hai danh sách và tìm giao điểm của chúng. Nói cách khác, chúng tôi muốn tìm các giá trị xuất hiện trong cả hai

Để làm điều đó, một lần nữa chúng ta có thể sử dụng

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
04 và lấy

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
0

Cách so sánh hai danh sách trong python và trả về sự khác biệt

Chúng ta có thể tìm sự khác biệt giữa hai danh sách trong python theo hai cách khác nhau

  • sử dụng
    In [1]: import numpy as np
    
    In [2]: numbers = [np.ones(3), np.zeros(2)]
    
    In [3]: numbers
    Out[3]: [array([1., 1., 1.]), array([0., 0.])]
    
    In [4]: target = [np.ones(3), np.zeros(2)]
    
    In [5]: numbers == target
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-5-b832db4b039d> in <module>
    ----> 1 numbers == target
    
    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    
    04
  • sử dụng thư viện the
    >>> numbers = []
    >>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
    >>> numbers.append(0.2) # add a single element
    >>> target = [0.3, 0.2]
    >>> numbers == target  # compares the lists
    False
    >>> numbers  # Ooopppssss....
    [0.30000000000000004, 0.2]
    >>> target
    [0.3, 0.2]
    
    9

Sử dụng In [1]: import numpy as np In [2]: numbers = [np.ones(3), np.zeros(2)] In [3]: numbers Out[3]: [array([1., 1., 1.]), array([0., 0.])] In [4]: target = [np.ones(3), np.zeros(2)] In [5]: numbers == target --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-5-b832db4b039d> in <module> ----> 1 numbers == target ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 04

So sánh danh sách hoạt động như thế nào trong Python

Giống như chúng ta đã làm để xác định giao điểm, chúng ta có thể tận dụng cấu trúc dữ liệu

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
04 để kiểm tra sự khác biệt giữa hai danh sách trong python

Nếu chúng ta muốn lấy tất cả các phần tử có trong danh sách đầu tiên nhưng không có trong danh sách thứ hai, chúng ta có thể sử dụng hàm

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
60

Mặt khác, nếu chúng ta muốn tìm tất cả các phần tử nằm trong một trong hai danh sách nhưng không phải cả hai, thì chúng ta có thể sử dụng

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
61

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
7

Phương pháp này có một hạn chế. nó nhóm những gì khác nhau giữa các danh sách thành một kết quả cuối cùng, đó là sự khác biệt đã đặt. Điều gì sẽ xảy ra nếu chúng ta muốn biết phần tử nào trong sự khác biệt đó thuộc về danh sách nào?

Sử dụng >>> numbers = [] >>> numbers.append(0.1 + 0.1 + 0.1) # derive the element based on a summation >>> numbers.append(0.2) # add a single element >>> target = [0.3, 0.2] >>> numbers == target # compares the lists False >>> numbers # Ooopppssss.... [0.30000000000000004, 0.2] >>> target [0.3, 0.2] 9

Như chúng ta đã thấy cho đến nay, thư viện này rất mạnh mẽ và nó trả về một sự khác biệt tốt đẹp. Hãy xem điều gì sẽ xảy ra khi chúng ta sử dụng

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
9 để lấy sự khác biệt giữa hai danh sách trong Python

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
0

Theo đó,

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
9 trả về những gì đã thay đổi từ danh sách này sang danh sách khác. Cách tiếp cận phù hợp sau đó sẽ phụ thuộc vào trường hợp sử dụng của bạn. Nếu bạn muốn có sự khác biệt chi tiết, hãy sử dụng
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
65. Nếu không, chỉ cần sử dụng một
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
04

Cách so sánh hai danh sách chuỗi

So sánh hai danh sách chuỗi trong Python phụ thuộc phần lớn vào loại so sánh bạn muốn thực hiện. Đó là bởi vì chúng ta có thể so sánh một chuỗi theo một số cách

Trong phần này, chúng ta sẽ thấy 3 cách khác nhau để làm điều đó

Cách đơn giản nhất là sử dụng toán tử

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4, như chúng ta đã thấy ở phần đầu. Phương pháp này phù hợp nếu bạn muốn so sánh chặt chẽ giữa từng chuỗi

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
1

Mọi thứ bắt đầu trở nên lộn xộn nếu bạn muốn so sánh danh sách các chuỗi nhưng bỏ qua trường hợp. Sử dụng

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4 cho điều đó không hiệu quả

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
2

Công cụ tốt nhất cho điều đó lại là

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
9. Nó cho phép chúng ta bỏ qua chuỗi bằng cách chuyển cờ boolean cho nó

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
3

Chúng ta cũng có thể bỏ qua thứ tự xuất hiện của các chuỗi trong danh sách

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
4

Bạn cũng có thể đi xa hơn và thực hiện so sánh nâng cao bằng cách chuyển một toán tử tùy chỉnh tới

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
65

Ví dụ: giả sử bạn muốn so sánh các chuỗi nhưng chúng có thể có

Hoặc có lẽ bạn muốn thực hiện bằng cách sử dụng chỉ số chỉnh sửa khoảng cách

Để làm điều đó, chúng ta có thể viết logic so sánh trong lớp toán tử và chuyển nó tới

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
65

Trong ví dụ đầu tiên này, chúng tôi sẽ bỏ qua mọi khoảng trắng bằng cách cắt các chuỗi trước khi so sánh chúng

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
5

Sau đó, chúng ta có thể cắm vào

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
65 bằng cách thêm nó vào danh sách
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
03, như vậy là
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
04

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
6

Cách so sánh hai danh sách từ điển

So sánh hai danh sách từ điển trong Python chắc chắn rất phức tạp nếu không có sự trợ giúp của thư viện bên ngoài. Như chúng ta đã thấy cho đến nay,

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
9 đủ linh hoạt và chúng ta có thể sử dụng nó để so sánh các đối tượng phức tạp sâu chẳng hạn như danh sách từ điển

Hãy xem điều gì sẽ xảy ra khi chúng ta chuyển hai danh sách từ điển

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
7

Nó xuất ra vị trí chính xác nơi các phần tử khác nhau và sự khác biệt là gì

Hãy xem một ví dụ khác trong đó một danh sách có một phần tử bị thiếu

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
8

Nó nói từ điển thứ hai đã bị xóa, đó là trường hợp của ví dụ này

Cách so sánh hai danh sách danh sách

So sánh các danh sách đa chiều—a. k. một danh sách các danh sách—thật dễ dàng cho

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
9. Nó hoạt động giống như một danh sách các
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
3

Trong ví dụ dưới đây, chúng tôi có hai danh sách đa chiều mà chúng tôi muốn so sánh. Khi được chuyển đến

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
65, nó sẽ trả về vị trí chính xác mà các phần tử khác nhau

Ví dụ: đối với vị trí

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
09, giá trị mới là 8 và giá trị cũ là 3. Một khía cạnh thú vị khác là nó hoạt động cho các cấu trúc lồng sâu, ví dụ,
>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
9 cũng làm nổi bật sự khác biệt ở vị trí
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
71

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
9

Khi cung cấp cho thư viện hai danh sách đa chiều giống hệt nhau, nó sẽ trả về một phản hồi trống

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
0

Cách so sánh hai danh sách đối tượng

Đôi khi chúng tôi có một danh sách các đối tượng tùy chỉnh mà chúng tôi muốn so sánh. Có lẽ chúng tôi muốn có sự khác biệt hoặc chỉ cần kiểm tra xem chúng có chứa các phần tử giống nhau không. Giải pháp cho vấn đề này không thể khác. sử dụng

>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
9

Ví dụ sau minh họa sức mạnh của thư viện này. Chúng ta sẽ so sánh hai danh sách có chứa một đối tượng tùy chỉnh và chúng ta sẽ có thể khẳng định xem chúng có bằng nhau hay không và sự khác biệt là gì

Trong ví dụ bên dưới, chúng tôi có hai danh sách đối tượng

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
73. Sự khác biệt duy nhất giữa hai là ở vị trí cuối cùng của đối tượng
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
73 có tuổi khác nhau.
>>> numbers = []
>>> numbers.append(0.1 + 0.1 + 0.1)  # derive the element based on a summation
>>> numbers.append(0.2) # add a single element
>>> target = [0.3, 0.2]
>>> numbers == target  # compares the lists
False
>>> numbers  # Ooopppssss....
[0.30000000000000004, 0.2]
>>> target
[0.3, 0.2]
9 không chỉ tìm đúng vị trí - ________ 576 - mà còn thấy rằng trường ________ 577 cũng khác

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
1

Cách so sánh hai danh sách các mảng có nhiều mảng

Trong phần này, chúng ta sẽ xem cách so sánh hai danh sách của mảng

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
6. Đây là một nhiệm vụ khá phổ biến đối với những người làm việc với khoa học dữ liệu và/hoặc học máy

Trong phần đầu tiên, chúng ta đã thấy rằng việc sử dụng toán tử

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4 không hoạt động tốt với danh sách các mảng
In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
6. May mắn thay, chúng ta có thể sử dụng. đoán xem. ?

Ví dụ bên dưới hiển thị hai danh sách có các mảng

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
6 khác nhau và thư viện có thể phát hiện vị trí chính xác mà chúng khác nhau. Làm thế nào là mát mẻ đó?

In [1]: import numpy as np

In [2]: numbers = [np.ones(3), np.zeros(2)]

In [3]: numbers
Out[3]: [array([1., 1., 1.]), array([0., 0.])]

In [4]: target = [np.ones(3), np.zeros(2)]

In [5]: numbers == target
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-b832db4b039d> in <module>
----> 1 numbers == target

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
2

Phần kết luận

Trong bài đăng này, chúng tôi đã thấy nhiều cách để so sánh hai danh sách trong Python. Phương pháp tốt nhất phụ thuộc vào loại yếu tố chúng ta có và cách chúng ta muốn so sánh. Hy vọng rằng, bây giờ bạn biết làm thế nào để

Python so sánh kích thước danh sách như thế nào?

Câu trả lời ngắn. Cách Pythonic nhất để kiểm tra xem hai danh sách có thứ tự l1 và l2 có giống nhau hay không là sử dụng toán tử l1 == l2 để so sánh từng phần tử . Nếu tất cả các phần tử đều bằng nhau và độ dài của danh sách giống nhau, thì giá trị trả về là True.

So sánh danh sách là gì?

So sánh là quá trình kiểm tra các mục dữ liệu của một mục dữ liệu khác trong danh sách, xem chúng có giống nhau hay không .

Làm cách nào để so sánh 4 danh sách trong Python?

Cách so sánh các danh sách trong Python .
Sử dụng hàm sort() và toán tử ==
Sử dụng các hàm map() và reduce()
Sử dụng hàm set() và toán tử ==
Sử dụng vòng lặp for
Sử dụng hàm set() và toán tử -
Sử dụng các bộ sưu tập. Lớp Counter() và toán tử ==
Sử dụng các hàm zip(), sum() và len()