Hướng dẫn how to check if two lists have same elements python - cách kiểm tra xem hai danh sách có phần tử giống nhau hay không. python

Xác định xem 2 danh sách có cùng yếu tố, bất kể đặt hàng?

Suy luận từ ví dụ của bạn:

x = ['a', 'b']
y = ['b', 'a']

rằng các yếu tố của danh sách sẽ không được lặp lại (chúng là duy nhất) cũng như có thể băm (chuỗi và các đối tượng python bất biến khác), câu trả lời hiệu quả trực tiếp và tính toán nhất Bộ bạn có thể đã học về ở trường).the most direct and computationally efficient answer uses Python's builtin sets, (which are semantically like mathematical sets you may have learned about in school).

set(x) == set(y) # prefer this if elements are hashable

Trong trường hợp các yếu tố có thể băm, nhưng không duy nhất,

set(x) == set(y) # prefer this if elements are hashable
0 cũng hoạt động về mặt ngữ nghĩa như một multiset, nhưng nó chậm hơn nhiều:

from collections import Counter
Counter(x) == Counter(y)

Thích sử dụng

set(x) == set(y) # prefer this if elements are hashable
1:

sorted(x) == sorted(y) 

Nếu các yếu tố có thể đặt hàng. Điều này sẽ giải thích cho các trường hợp không duy nhất hoặc không thể đánh bại, nhưng điều này có thể chậm hơn nhiều so với sử dụng các bộ.

Thí nghiệm thực nghiệm

Một thí nghiệm thực nghiệm kết luận rằng người ta nên thích

set(x) == set(y) # prefer this if elements are hashable
2, sau đó
set(x) == set(y) # prefer this if elements are hashable
1. Chỉ chọn cho
set(x) == set(y) # prefer this if elements are hashable
4 nếu bạn cần những thứ khác như tính hoặc sử dụng thêm như một đa.

Thiết lập đầu tiên:

import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)

Và kiểm tra:

>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844

Vì vậy, chúng tôi thấy rằng so sánh các bộ là giải pháp nhanh nhất và so sánh danh sách được sắp xếp là nhanh thứ hai.

Làm thế nào để bạn tìm thấy các yếu tố tương tự trong hai danh sách trong Python?

Phương pháp 2: Sử dụng thuộc tính Giao lộ của Set Chuyển đổi danh sách thành được đặt bằng cách chuyển đổi. Sử dụng chức năng giao nhau để kiểm tra xem cả hai bộ có bất kỳ yếu tố chung nào không. Nếu chúng có nhiều yếu tố chung, thì hãy in giao điểm của cả hai bộ.

set(x) == set(y) # prefer this if elements are hashable
7 coupled with
set(x) == set(y) # prefer this if elements are hashable
8 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.

set(x) == set(y) # prefer this if elements are hashable
9
from collections import Counter
Counter(x) == Counter(y)
0
from collections import Counter
Counter(x) == Counter(y)
1
from collections import Counter
Counter(x) == Counter(y)
2223
from collections import Counter
Counter(x) == Counter(y)
4
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
6
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
8
from collections import Counter
Counter(x) == Counter(y)
3
sorted(x) == sorted(y) 
03031

sorted(x) == sorted(y) 
2
from collections import Counter
Counter(x) == Counter(y)
0
from collections import Counter
Counter(x) == Counter(y)
1
from collections import Counter
Counter(x) == Counter(y)
2223
from collections import Counter
Counter(x) == Counter(y)
4
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
6
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
8
from collections import Counter
Counter(x) == Counter(y)
3
sorted(x) == sorted(y) 
03031

import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
7
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
8
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
9
>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
0

import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
3
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
8
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
9
>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
6

>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
7

>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
8

>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
9
set(x) == set(y) # prefer this if elements are hashable
76
set(x) == set(y) # prefer this if elements are hashable
77___

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

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
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

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
9
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
4
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
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

Đầu ra:

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

Phương pháp 4: Sử dụng công suất khớp nối

from collections import Counter
Counter(x) == Counter(y)
12Care của
from collections import Counter
Counter(x) == Counter(y)
13 cho các giá trị và tiện ích của
from collections import Counter
Counter(x) == Counter(y)
14, chúng ta có thể đạt được nhiệm vụ này là kiểm tra sự bình đẳng của hai danh sách là giống hệt nhau. Điều này cũng tính đến việc đặt hàng của danh sách.

Using
The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
7, 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.

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
8
from collections import Counter
Counter(x) == Counter(y)
16

set(x) == set(y) # prefer this if elements are hashable
9
from collections import Counter
Counter(x) == Counter(y)
0
from collections import Counter
Counter(x) == Counter(y)
1
from collections import Counter
Counter(x) == Counter(y)
2223
from collections import Counter
Counter(x) == Counter(y)
4
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
6
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
8
from collections import Counter
Counter(x) == Counter(y)
3
sorted(x) == sorted(y) 
03031

sorted(x) == sorted(y) 
2
from collections import Counter
Counter(x) == Counter(y)
0
from collections import Counter
Counter(x) == Counter(y)
1
from collections import Counter
Counter(x) == Counter(y)
2223
from collections import Counter
Counter(x) == Counter(y)
4
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
6
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
8
from collections import Counter
Counter(x) == Counter(y)
3
sorted(x) == sorted(y) 
03031

import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
7
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
8
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
9
>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
0

import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
3
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
8
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
9
>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
6

>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
9
set(x) == set(y) # prefer this if elements are hashable
19
from collections import Counter
Counter(x) == Counter(y)
0__
set(x) == set(y) # prefer this if elements are hashable
22

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

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
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

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
9
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
4
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
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

Đầu ra:

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

Phương pháp 3: Sử dụng

set(x) == set(y) # prefer this if elements are hashable
35 sử dụng
set(x) == set(y) # prefer this if elements are hashable
36, chúng ta có thể nhận được tổng của một trong danh sách là tổng 1 nếu cả hai chỉ mục trong hai danh sách có các phần tử bằng nhau, sau đó so sánh số đó với kích thước của danh sách khác. Điều này cũng yêu cầu trước tiên để kiểm tra xem hai danh sách có bằng nhau trước khi tính toán này không. Nó cũng kiểm tra đơn hàng.

Using
set(x) == set(y) # prefer this if elements are hashable
36, 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.

set(x) == set(y) # prefer this if elements are hashable
9
from collections import Counter
Counter(x) == Counter(y)
0
from collections import Counter
Counter(x) == Counter(y)
1
from collections import Counter
Counter(x) == Counter(y)
2223
from collections import Counter
Counter(x) == Counter(y)
4
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
6
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
8
from collections import Counter
Counter(x) == Counter(y)
3
sorted(x) == sorted(y) 
03031

sorted(x) == sorted(y) 
2
from collections import Counter
Counter(x) == Counter(y)
0
from collections import Counter
Counter(x) == Counter(y)
1
from collections import Counter
Counter(x) == Counter(y)
2223
from collections import Counter
Counter(x) == Counter(y)
4
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
6
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
8
from collections import Counter
Counter(x) == Counter(y)
3
sorted(x) == sorted(y) 
03031

import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
7
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
8
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
9
>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
0

import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
3
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
8
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
9
>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
6

>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
9
set(x) == set(y) # prefer this if elements are hashable
76
set(x) == set(y) # prefer this if elements are hashable
77___

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

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
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

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
9
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
4
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
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

Đầu ra:

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

Phương pháp 3: Sử dụng

set(x) == set(y) # prefer this if elements are hashable
35 sử dụng
set(x) == set(y) # prefer this if elements are hashable
36, chúng ta có thể nhận được tổng của một trong danh sách là tổng 1 nếu cả hai chỉ mục trong hai danh sách có các phần tử bằng nhau, sau đó so sánh số đó với kích thước của danh sách khác. Điều này cũng yêu cầu trước tiên để kiểm tra xem hai danh sách có bằng nhau trước khi tính toán này không. Nó cũng kiểm tra đơn hàng.

Carefully coupling power of
from collections import Counter
Counter(x) == Counter(y)
13to hash values and utility of
from collections import Counter
Counter(x) == Counter(y)
14, 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.

set(x) == set(y) # prefer this if elements are hashable
9
from collections import Counter
Counter(x) == Counter(y)
0
from collections import Counter
Counter(x) == Counter(y)
1
from collections import Counter
Counter(x) == Counter(y)
2223
from collections import Counter
Counter(x) == Counter(y)
4
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
6
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
8
from collections import Counter
Counter(x) == Counter(y)
3
sorted(x) == sorted(y) 
03031

set(x) == set(y) # prefer this if elements are hashable
9
from collections import Counter
Counter(x) == Counter(y)
0
from collections import Counter
Counter(x) == Counter(y)
1
from collections import Counter
Counter(x) == Counter(y)
2223
from collections import Counter
Counter(x) == Counter(y)
4
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
6
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
8
from collections import Counter
Counter(x) == Counter(y)
3
sorted(x) == sorted(y) 
03031

sorted(x) == sorted(y) 
2
from collections import Counter
Counter(x) == Counter(y)
0
from collections import Counter
Counter(x) == Counter(y)
1
from collections import Counter
Counter(x) == Counter(y)
2223
from collections import Counter
Counter(x) == Counter(y)
4
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
6
from collections import Counter
Counter(x) == Counter(y)
3
from collections import Counter
Counter(x) == Counter(y)
8
from collections import Counter
Counter(x) == Counter(y)
3
sorted(x) == sorted(y) 
03031

import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
7
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
8
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
9
>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
0

import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
3
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
8
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
9
>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
6

>>> min(timeit.repeat(sets_equal))
13.976069927215576
>>> min(timeit.repeat(counters_equal))
73.17287588119507
>>> min(timeit.repeat(sorted_lists_equal))
36.177085876464844
9
set(x) == set(y) # prefer this if elements are hashable
76
set(x) == set(y) # prefer this if elements are hashable
77___

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

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
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

The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical
9
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
4
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
5
import timeit
import random
from collections import Counter

data = [str(random.randint(0, 100000)) for i in xrange(100)]
data2 = data[:]     # copy the list into a new one

def sets_equal(): 
    return set(data) == set(data2)

def counters_equal(): 
    return Counter(data) == Counter(data2)

def sorted_lists_equal(): 
    return sorted(data) == sorted(data2)
6
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

Đầu ra:

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

Làm thế nào để bạn kiểm tra xem danh sách có cùng yếu tố Python không?

Sử dụng Count (), Phương thức danh sách Python () trả về số lượng một phần tử xảy ra trong danh sách.Vì vậy, nếu chúng ta có cùng một phần tử được lặp lại trong danh sách thì độ dài của danh sách sử dụng Len () sẽ giống như số lần phần tử có trong danh sách bằng cách sử dụng Count (). The python list method count() returns count of how many times an element occurs in list. So if we have the same element repeated in the list then the length of the list using len() will be same as the number of times the element is present in the list using the count().

Làm thế nào để bạn tìm thấy các yếu tố tương tự trong hai danh sách trong Python?

Phương pháp 2: Sử dụng thuộc tính Giao lộ của Set Chuyển đổi danh sách thành được đặt bằng cách chuyển đổi.Sử dụng chức năng giao nhau để kiểm tra xem cả hai bộ có bất kỳ yếu tố chung nào không.Nếu chúng có nhiều yếu tố chung, thì hãy in giao điểm của cả hai bộ.Using Set's intersection property Convert the list to set by conversion. Use the intersection function to check if both sets have any elements in common. If they have many elements in common, then print the intersection of both sets.

Làm thế nào để bạn biết nếu một danh sách có cùng yếu tố?

Một cách tiếp cận khác để tìm kiếm, nếu hai danh sách có các yếu tố phổ biến là sử dụng các bộ.Các bộ có bộ sưu tập không có thứ tự của các yếu tố độc đáo.Vì vậy, chúng tôi chuyển đổi các danh sách thành các tập hợp và sau đó tạo một bộ mới bằng cách kết hợp các bộ đã cho.Nếu chúng có một số yếu tố phổ biến thì bộ mới sẽ không trống.use sets. The sets have unordered collection of unique elements. So we convert the lists into sets and then create a new set by combining the given sets. If they have some common elements then the new set will not be empty.