Hướng dẫn how do you compare two values in python and return non matches? - làm cách nào để so sánh hai giá trị trong python và trả về không trùng khớp?

Tôi có thể dựa vào stdlib ở đây ...

from itertools import tee, izip
def pairwise[iterable]:
    "s -> [s0,s1], [s1,s2], [s2, s3], ..."
    a, b = tee[iterable]
    next[b, None]
    return izip[a, b]

import difflib

def returnNotMatches[a, b]:
    blocks = difflib.SequenceMatcher[a=a, b=b].get_matching_blocks[]
    differences = []
    for b1, b2 in pairwise[blocks]:
        d1 = a[b1.a + b1.size: b2.a]
        d2 = b[b1.b + b1.size: b2.b]
        differences.append[[d1, d2]]
    return differences

print returnNotMatches[[ 1,2,3,4,5 ], [ 1,2,3,6 ]]

Bản in nào:

import functools 


l1 = [10, 20, 30, 40, 50] 
l2 = [10, 20, 30, 50, 40, 70] 
l3 = [10, 20, 30, 40, 50] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l2], True]: 
    print ["The lists l1 and l2 are the same"] 
else: 
    print ["The lists l1 and l2 are not the same"] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l3], True]: 
    print ["The lists l1 and l3 are the same"] 
else: 
    print ["The lists l1 and l3 are not the same"] 

1

Điều này so sánh các chuỗi như các luồng và tìm thấy sự khác biệt trong các luồng. Nó có thể vào tài khoản, v.v. Nếu đặt hàng và nhân đôi không quan trọng, thì

import functools 


l1 = [10, 20, 30, 40, 50] 
l2 = [10, 20, 30, 50, 40, 70] 
l3 = [10, 20, 30, 40, 50] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l2], True]: 
    print ["The lists l1 and l2 are the same"] 
else: 
    print ["The lists l1 and l2 are not the same"] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l3], True]: 
    print ["The lists l1 and l3 are the same"] 
else: 
    print ["The lists l1 and l3 are not the same"] 

2 là con đường để đi [miễn là các yếu tố có thể được băm].

Trong bài viết này, chúng tôi sẽ hiểu các cách khác nhau để so sánh hai danh sách trong Python. Chúng ta thường bắt gặp các tình huống trong đó chúng ta cần so sánh các giá trị của các mục dữ liệu được lưu trữ trong bất kỳ cấu trúc nào nói danh sách, tuple, chuỗi, v.v.compare two lists in Python. We often come across situations wherein we need to compare the values of the data items stored in any structure say list, tuple, string, etc.

import functools 


l1 = [10, 20, 30, 40, 50] 
l2 = [10, 20, 30, 50, 40, 70] 
l3 = [10, 20, 30, 40, 50] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l2], True]: 
    print ["The lists l1 and l2 are the same"] 
else: 
    print ["The lists l1 and l2 are not the same"] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l3], True]: 
    print ["The lists l1 and l3 are the same"] 
else: 
    print ["The lists l1 and l3 are not the same"] 

3 là phương pháp kiểm tra các mục dữ liệu của danh sách chống lại sự bình đẳng với các mục dữ liệu của danh sách khác.

Phương pháp so sánh hai danh sách trong Python

Chúng tôi có thể sử dụng một trong các phương pháp sau để thực hiện so sánh của chúng tôi:

  • Hàm giảm [] và map []
  • Chức năng thu gom.cunt []
  • Hàm python sort [] cùng với == toán tử
  • Hàm python set [] cùng với == toán tử
  • Hàm khác biệt []

1. Hàm giảm python [] và map []

Chúng ta có thể sử dụng hàm python map [] cùng với hàm functools.reduce [] để so sánh các mục dữ liệu của hai danh sách.along with functools.reduce[] function to compare the data items of two lists.

Phương thức

import functools 


l1 = [10, 20, 30, 40, 50] 
l2 = [10, 20, 30, 50, 40, 70] 
l3 = [10, 20, 30, 40, 50] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l2], True]: 
    print ["The lists l1 and l2 are the same"] 
else: 
    print ["The lists l1 and l2 are not the same"] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l3], True]: 
    print ["The lists l1 and l3 are the same"] 
else: 
    print ["The lists l1 and l3 are not the same"] 

4 chấp nhận một hàm và một điều khác nhau như danh sách, tuple, chuỗi, v.v. làm đối số.function and an iterable such as list, tuple, string, etc. as arguments.

Nó áp dụng hàm được truyền cho từng mục của IT có thể lặp lại và sau đó trả về một đối tượng bản đồ, tức là một trình lặp lại như là kết quả..

Phương pháp

import functools 


l1 = [10, 20, 30, 40, 50] 
l2 = [10, 20, 30, 50, 40, 70] 
l3 = [10, 20, 30, 40, 50] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l2], True]: 
    print ["The lists l1 and l2 are the same"] 
else: 
    print ["The lists l1 and l2 are not the same"] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l3], True]: 
    print ["The lists l1 and l3 are the same"] 
else: 
    print ["The lists l1 and l3 are not the same"] 

5 áp dụng hàm được truyền cho mọi yếu tố của đầu vào có thể lặp lại theo cách đệ quy.recursive manner.

Ban đầu, nó sẽ áp dụng hàm trên phần tử thứ nhất và phần hai và trả về kết quả. Quá trình tương tự sẽ tiếp tục trên từng phần tử cho đến khi danh sách không còn phần tử.

Là một kết hợp, hàm map [] sẽ áp dụng hàm đầu vào cho mọi phần tử và hàm giảm [] sẽ đảm bảo rằng nó áp dụng chức năng theo cách liên tiếp.consecutive manner.

Example:

import functools 


l1 = [10, 20, 30, 40, 50] 
l2 = [10, 20, 30, 50, 40, 70] 
l3 = [10, 20, 30, 40, 50] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l2], True]: 
    print ["The lists l1 and l2 are the same"] 
else: 
    print ["The lists l1 and l2 are not the same"] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l3], True]: 
    print ["The lists l1 and l3 are the same"] 
else: 
    print ["The lists l1 and l3 are not the same"] 

Output:

The lists l1 and l2 are not the same
The lists l1 and l3 are the same

2. Phương thức Python Collection.Count []

Phương thức Collection.Count [] có thể được sử dụng để so sánh danh sách một cách hiệu quả. Hàm bộ đếm [] đếm tần số của các mục trong danh sách và lưu dữ liệu làm từ điển ở định dạng:.:.

Nếu hai danh sách có cùng một đầu ra từ điển, chúng ta có thể suy ra rằng các danh sách là như nhau.

Lưu ý: Thứ tự danh sách không có hiệu lực đối với phương thức bộ đếm []. The list order has no effect on the counter[] method.

Example:

import collections 


l1 = [10, 20, 30, 40, 50] 
l2 = [10, 20, 30, 50, 40, 70] 
l3 = [10, 20, 30, 40, 50] 

if collections.Counter[l1] == collections.Counter[l2]:
    print ["The lists l1 and l2 are the same"] 
else: 
    print ["The lists l1 and l2 are not the same"] 

if collections.Counter[l1] == collections.Counter[l3]:
    print ["The lists l1 and l3 are the same"] 
else: 
    print ["The lists l1 and l3 are not the same"] 

Output:

The lists l1 and l2 are not the same
The lists l1 and l3 are the same

3. Phương thức Python Sort [] và == Toán tử để so sánh danh sách

Chúng ta có thể câu lạc bộ phương thức Python Sắp xếp [] với toán tử == để so sánh hai danh sách.== operator to compare two lists.

Phương thức Python

import functools 


l1 = [10, 20, 30, 40, 50] 
l2 = [10, 20, 30, 50, 40, 70] 
l3 = [10, 20, 30, 40, 50] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l2], True]: 
    print ["The lists l1 and l2 are the same"] 
else: 
    print ["The lists l1 and l2 are not the same"] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l3], True]: 
    print ["The lists l1 and l3 are the same"] 
else: 
    print ["The lists l1 and l3 are not the same"] 

6 được sử dụng để sắp xếp các danh sách đầu vào với mục đích nếu hai danh sách đầu vào bằng nhau, thì các phần tử sẽ nằm ở cùng một vị trí chỉ mục.sort the input lists with a purpose that if the two input lists are equal, then the elements would reside at the same index positions.

Lưu ý: Thứ tự của danh sách không ảnh hưởng đến phương pháp này vì chúng tôi sẽ sắp xếp các danh sách trước khi so sánh. The order of the list does not affect this method because we’ll be sorting the lists before comparison.

Hơn nữa,

import functools 


l1 = [10, 20, 30, 40, 50] 
l2 = [10, 20, 30, 50, 40, 70] 
l3 = [10, 20, 30, 40, 50] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l2], True]: 
    print ["The lists l1 and l2 are the same"] 
else: 
    print ["The lists l1 and l2 are not the same"] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l3], True]: 
    print ["The lists l1 and l3 are the same"] 
else: 
    print ["The lists l1 and l3 are not the same"] 

7 được sử dụng để so sánh danh sách, phần tử theo phần tử.

Example:

import collections 


l1 = [10, 20, 30, 40, 50] 
l2 = [10, 20, 30, 50, 40, 70] 
l3 = [50, 10, 30, 20, 40] 

l1.sort[] 
l2.sort[] 
l3.sort[] 

if l1 == l3: 
    print ["The lists l1 and l3 are the same"] 
else: 
    print ["The lists l1 and l3 are not the same"] 


if l1 == l2: 
    print ["The lists l1 and l2 are the same"] 
else: 
    print ["The lists l1 and l2 are not the same"] 

Output:

The lists l1 and l3 are the same
The lists l1 and l2 are not the same

4. Phương thức Python Set [] và == Toán tử để so sánh hai danh sách

Phương thức Python Set [] thao tác các mục dữ liệu của một bộ điều chỉnh được sắp xếp cho một tập hợp các mục dữ liệu được sắp xếp theo thứ tự các phần tử được xem xét.sorted sequence set of data items without taking the order of elements into consideration.

Hơn nữa,

import functools 


l1 = [10, 20, 30, 40, 50] 
l2 = [10, 20, 30, 50, 40, 70] 
l3 = [10, 20, 30, 40, 50] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l2], True]: 
    print ["The lists l1 and l2 are the same"] 
else: 
    print ["The lists l1 and l2 are not the same"] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l3], True]: 
    print ["The lists l1 and l3 are the same"] 
else: 
    print ["The lists l1 and l3 are not the same"] 

7 được sử dụng để so sánh các mục dữ liệu của danh sách theo kiểu yếu tố.

Example:

l1 = [10, 20, 30, 40, 50] 
l3 = [50, 10, 30, 20, 40] 

a = set[l1]
b = set[l3]

if a == b:
    print["Lists l1 and l3 are equal"]
else:
    print["Lists l1 and l3 are not equal"]

Output:

Lists l1 and l3 are equal

5. Danh sách tùy chỉnh Python Hiểu để so sánh hai danh sách

Chúng ta có thể sử dụng khả năng hiểu danh sách Python để so sánh hai danh sách.

Example:

l1 = [10, 20, 30, 40, 50] 
l3 = [50, 75, 30, 20, 40, 69] 

res = [x for x in l1 + l3 if x not in l1 or x not in l3]

print[res]
if not res:
    print["Lists l1 and l3 are equal"]
else:
    print["Lists l1 and l3 are not equal"]

 

Trong mã trên, chúng tôi đặt một phần tử con trỏ ‘X, thành danh sách L1 và L3. Hơn nữa, chúng tôi kiểm tra xem phần tử được trỏ bởi phần tử con trỏ có trong danh sách.

Output:

import functools 


l1 = [10, 20, 30, 40, 50] 
l2 = [10, 20, 30, 50, 40, 70] 
l3 = [10, 20, 30, 40, 50] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l2], True]: 
    print ["The lists l1 and l2 are the same"] 
else: 
    print ["The lists l1 and l2 are not the same"] 

if functools.reduce[lambda x, y : x and y, map[lambda p, q: p == q,l1,l3], True]: 
    print ["The lists l1 and l3 are the same"] 
else: 
    print ["The lists l1 and l3 are not the same"] 

0

Sự kết luận

Do đó, trong bài viết này, chúng tôi đã đề cập và hiểu một số cách để so sánh nhiều danh sách trong Python.

Làm thế nào để bạn có được các yếu tố không phù hợp từ hai danh sách trong Python?

Sử dụng cho vòng lặp, chúng tôi có thể thiết kế cho Loop để so sánh các giá trị ở các chỉ mục tương tự. ID các giá trị không khớp sau đó chúng tôi thêm chỉ mục vào danh sách kết quả. Vòng lặp FOR trước tiên tìm nạp giá trị trong chỉ mục đầu tiên và sau đó sử dụng điều kiện IF để so sánh nó với các giá trị từ danh sách thứ hai. we can design for loop to compare the values at similar indexes. Id the values do not match then we add the index to a result list. The for loop first fetches the value in the first index and then uses an if condition to compare it with values from the second list.

Làm thế nào tôi có thể so sánh hai danh sách trong Python và Return Chênh lệch?

Có thể tìm thấy sự khác biệt giữa hai danh sách [giả sử List1 và List2] bằng cách sử dụng hàm đơn giản sau.Bằng cách sử dụng hàm trên, sự khác biệt có thể được tìm thấy bằng cách sử dụng Diff [TEMP2, TEMP1] hoặc DIFT [TEMP1, TEMP2].Cả hai sẽ cho kết quả ['bốn', 'ba'].diff[temp2, temp1] or diff[temp1, temp2] . Both will give the result ['Four', 'Three'] .

Làm cách nào để so sánh hai giá trị danh sách trong Python?

Phương thức python sort [] và == Toán tử để so sánh các danh sách chúng ta có thể câu lạc bộ phương thức python sort [] với toán tử == để so sánh hai danh sách.Phương thức python sort [] được sử dụng để sắp xếp các danh sách đầu vào với mục đích nếu hai danh sách đầu vào bằng nhau, thì các phần tử sẽ nằm ở cùng một vị trí chỉ mục. to compare lists We can club the Python sort[] method with the == operator to compare two lists. Python sort[] method is used to sort the input lists with a purpose that if the two input lists are equal, then the elements would reside at the same index positions.

Làm thế nào để bạn so sánh kết quả trong Python?

== và là hai cách để so sánh các đối tượng trong Python.== So sánh 2 đối tượng cho sự bình đẳng và là so sánh 2 đối tượng cho danh tính ...
Ví dụ 1 so sánh 2 chuỗi.....
Ví dụ 2 tạo danh sách A và B cuối cùng đề cập đến cùng một đối tượng ..

Bài Viết Liên Quan

Chủ Đề