Hướng dẫn how to check if two lists of strings are equal in python - cách kiểm tra xem hai danh sách chuỗi có bằng nhau trong python không

Python không có một phương pháp trực tiếp để so sánh danh sách. Nhưng có nhiều cách để so sánh hai danh sách các chuỗi trong Python.

Các phương pháp sau để thực hiện so sánh danh sách:

  • Giảm () và MAP () Hàm
  • Phương thức thu gom.Count ()
  • Sắp xếp () Phương thức và == Toán tử
  • SET () Phương thức và == Toán tử
  • Danh sách tùy chỉnh hiểu

Lưu ý: Hàm cmp () không sử dụng trong phiên bản Python 3.x.The cmp() function doesn’t use in Python 3.x version.

Python so sánh hai danh sách các chuỗi ví dụ

Mã ví dụ đơn giản với tất cả các cách có thể.

Sử dụng các hàm giảm () và map ()

import functools

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']


if functools.reduce(lambda x, y: x and y, map(lambda a, b: a == b, list1, list2), True):
    print("Both List are same")
else:
    print("Not same")

Output::

Hướng dẫn how to check if two lists of strings are equal in python - cách kiểm tra xem hai danh sách chuỗi có bằng nhau trong python không

Phương thức Python Collection.Count ()

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:.:.

import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")

Đầu ra: không giống nhau: Not the same

Sử dụng phương thức sort () và == toán tử để so sánh danh sách

Danh sách được sắp xếp và toán tử == được sử dụng để so sánh danh sách, phần tử theo phần tử.

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

list1.sort()
list2.sort()

if list1 == list2:
    print("Both List are the same")
else:
    print("Not same")

Đầu ra: cả hai danh sách đều giống nhauBoth List are the same

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

Toán tử bằng nhau == đượ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ố.

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")

Đầu ra: cả hai danh sách đều giống nhau: Both List are the same

Sử dụng danh sách tùy chỉnh hiểu để so sánh hai danh sách

Nếu danh sách chuỗi giống nhau thì danh sách có các phần tử bằng không.

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)

Đầu ra: []: []

Hãy bình luận nếu bạn có bất kỳ nghi ngờ hoặc đề xuất nào về mã chủ đề danh sách Python này.

Lưu ý: IDE: & NBSP; Pycharm & NBSP; 2021.3.3 (Phiên bản cộng đồng) IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

Tất cả & nbsp; ví dụ python & nbsp; là trong & nbsp; Python & nbsp; 3, vì vậy có thể khác với các phiên bản Python 2 hoặc nâng cấp. Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Hướng dẫn how to check if two lists of strings are equal in python - cách kiểm tra xem hai danh sách chuỗi có bằng nhau trong python không

Bằng cấp về Khoa học máy tính và Kỹ sư: Nhà phát triển ứng dụng và có nhiều ngôn ngữ lập trình kinh nghiệm. Sự nhiệt tình cho công nghệ và thích học kỹ thuật.

Bài viết này liên quan đến nhiệm vụ của các cách để kiểm tra xem hai danh sách không có thứ tự có chứa các yếu tố tương tự chính xác ở vị trí tương tự chính xác hay không, tức là kiểm tra xem hai danh sách có chính xác không. Đây là một tiện ích khá hữu ích và có thể được sử dụng trong lập trình hàng ngày.

Phương pháp 1: Sử dụng toán tử list.sort()

import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
0 được kết hợp với toán tử ____ có thể đạt được nhiệm vụ này. Trước tiên chúng tôi sắp xếp danh sách, để nếu cả hai danh sách đều giống hệt nhau, thì chúng có các phần tử ở cùng một vị trí. Nhưng điều này không có tính đến việc đặt hàng các yếu tố trong danh sách.
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
1 coupled with
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
2 operator can achieve this task. We first sort the list, so that if both the lists are identical, then they have elements at the same position. But this doesn’t take into account the ordering of elements in list.

import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
3
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
5
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
6
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
7
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
8__

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

list1.sort()
list2.sort()

if list1 == list2:
    print("Both List are the same")
else:
    print("Not same")
6
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
5
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
6
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
7
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
8__

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
1
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
2
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
3
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
4

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
7
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
2
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
3
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
0

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
1

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
2

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
3
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
3
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
7

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
8
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
1
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
2

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
3
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
4

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
8
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
8
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
2

Đầu ra:

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical

Bài viết này liên quan đến nhiệm vụ của các cách để kiểm tra xem hai danh sách không có thứ tự có chứa các yếu tố tương tự chính xác ở vị trí tương tự chính xác hay không, tức là kiểm tra xem hai danh sách có chính xác không. Đây là một tiện ích khá hữu ích và có thể được sử dụng trong lập trình hàng ngày.
Using

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
1, we usually are able to get frequency of each element in list, checking for it, for both the list, we can check if two lists are identical or not. But this method also ignores the ordering of the elements in the list and only takes into account the frequency of elements.

Phương pháp 1: Sử dụng toán tử list.sort()

import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
0 được kết hợp với toán tử ____ có thể đạt được nhiệm vụ này. Trước tiên chúng tôi sắp xếp danh sách, để nếu cả hai danh sách đều giống hệt nhau, thì chúng có các phần tử ở cùng một vị trí. Nhưng điều này không có tính đến việc đặt hàng các yếu tố trong danh sách.

import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
3
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
5
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
6
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
7
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
8__

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

list1.sort()
list2.sort()

if list1 == list2:
    print("Both List are the same")
else:
    print("Not same")
6
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
5
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
6
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
7
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
8__

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
1
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
2
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
3
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
4

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
7
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
2
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
3
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
0

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
3
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
3
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
7

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
8
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
1
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
2

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
3
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
4

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
8
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
8
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
2

Đầu ra:

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical

Bài viết này liên quan đến nhiệm vụ của các cách để kiểm tra xem hai danh sách không có thứ tự có chứa các yếu tố tương tự chính xác ở vị trí tương tự chính xác hay không, tức là kiểm tra xem hai danh sách có chính xác không. Đây là một tiện ích khá hữu ích và có thể được sử dụng trong lập trình hàng ngày.
Using

import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
30, we can get sum of one of the list as summation of 1 if both the index in two lists have equal elements, and then compare that number with size of other list. This also requires first to check if two lists are equal before this computation. It also checks for the order.

import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
3
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
5
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
6
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
7
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
8__

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

list1.sort()
list2.sort()

if list1 == list2:
    print("Both List are the same")
else:
    print("Not same")
6
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
5
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
6
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
7
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
8__

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
1
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
2
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
3
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
4

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
7
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
2
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
3
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
0

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
3
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
3
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
7

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
8
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
1
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
2

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
3
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
4

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
8
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
8
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
2

Đầu ra:

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical

Bài viết này liên quan đến nhiệm vụ của các cách để kiểm tra xem hai danh sách không có thứ tự có chứa các yếu tố tương tự chính xác ở vị trí tương tự chính xác hay không, tức là kiểm tra xem hai danh sách có chính xác không. Đây là một tiện ích khá hữu ích và có thể được sử dụng trong lập trình hàng ngày.
Carefully coupling power of

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

list1.sort()
list2.sort()

if list1 == list2:
    print("Both List are the same")
else:
    print("Not same")
07to hash values and utility of
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

list1.sort()
list2.sort()

if list1 == list2:
    print("Both List are the same")
else:
    print("Not same")
08, we can achieve this task of checking for equality of two lists to be identical. This also takes into account the ordering of the list.

Phương pháp 1: Sử dụng toán tử list.sort()

import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
0 được kết hợp với toán tử ____ có thể đạt được nhiệm vụ này. Trước tiên chúng tôi sắp xếp danh sách, để nếu cả hai danh sách đều giống hệt nhau, thì chúng có các phần tử ở cùng một vị trí. Nhưng điều này không có tính đến việc đặt hàng các yếu tố trong danh sách.

import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
3
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
5
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
6
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
7
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
8__

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

list1.sort()
list2.sort()

if list1 == list2:
    print("Both List are the same")
else:
    print("Not same")
6
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
4
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
5
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
6
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
7
import collections

list1 = ['A', 'B', 'C']
list2 = ['A', 'D', 'E']

if collections.Counter(list1) == collections.Counter(list2):
    print("Both List are same")
else:
    print("Not the same")
8__

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
1
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
2
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
3
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
4

list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
7
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
2
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
3
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
0

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
3
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

list1.sort()
list2.sort()

if list1 == list2:
    print("Both List are the same")
else:
    print("Not same")
50
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

list1.sort()
list2.sort()

if list1 == list2:
    print("Both List are the same")
else:
    print("Not same")
51
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

list1.sort()
list2.sort()

if list1 == list2:
    print("Both List are the same")
else:
    print("Not same")
53
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

list1.sort()
list2.sort()

if list1 == list2:
    print("Both List are the same")
else:
    print("Not same")
54__

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
8
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
1
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
2

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
3
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
4

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
8
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

s1 = set(list1)
s2 = set(list2)

if s1 == s2:
    print("Both List are the same")
else:
    print("Not same")
9
list1 = ['A', 'B', 'C']
list2 = ['A', 'C', 'B']

res = [x for x in list1 + list2 if x not in list1 or x not in list2]

print(res)
0
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
8
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
2

Đầu ra:

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical