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 withset[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
9from collections import Counter
Counter[x] == Counter[y]
0 from collections import Counter
Counter[x] == Counter[y]
1from collections import Counter
Counter[x] == Counter[y]
2223from collections import Counter
Counter[x] == Counter[y]
4from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
6from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
8from collections import Counter
Counter[x] == Counter[y]
3sorted[x] == sorted[y]
03031sorted[x] == sorted[y]
2from collections import Counter
Counter[x] == Counter[y]
0 from collections import Counter
Counter[x] == Counter[y]
1from collections import Counter
Counter[x] == Counter[y]
2223from collections import Counter
Counter[x] == Counter[y]
4from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
6from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
8from collections import Counter
Counter[x] == Counter[y]
3sorted[x] == sorted[y]
03031import 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]
6import 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
0import 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
76set[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 identical4
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]
6The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical7
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical8
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical9
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical0
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical4
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]
6The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical4
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical8
Đầ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
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.from collections import Counter
Counter[x] == Counter[y]
Using
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical7, 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 identical8
from collections import Counter
Counter[x] == Counter[y]
16set[x] == set[y] # prefer this if elements are hashable
9from collections import Counter
Counter[x] == Counter[y]
0 from collections import Counter
Counter[x] == Counter[y]
1from collections import Counter
Counter[x] == Counter[y]
2223from collections import Counter
Counter[x] == Counter[y]
4from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
6from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
8from collections import Counter
Counter[x] == Counter[y]
3sorted[x] == sorted[y]
03031sorted[x] == sorted[y]
2from collections import Counter
Counter[x] == Counter[y]
0 from collections import Counter
Counter[x] == Counter[y]
1from collections import Counter
Counter[x] == Counter[y]
2223from collections import Counter
Counter[x] == Counter[y]
4from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
6from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
8from collections import Counter
Counter[x] == Counter[y]
3sorted[x] == sorted[y]
03031import 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]
6import 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
0import 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
19from collections import Counter
Counter[x] == Counter[y]
0__ set[x] == set[y] # prefer this if elements are hashable
22The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical4
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]
6The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical7
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical8
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical9
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical0
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical4
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]
6The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical4
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical8
Đầ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
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.set[x] == set[y] # prefer this if elements are hashable
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
9from collections import Counter
Counter[x] == Counter[y]
0 from collections import Counter
Counter[x] == Counter[y]
1from collections import Counter
Counter[x] == Counter[y]
2223from collections import Counter
Counter[x] == Counter[y]
4from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
6from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
8from collections import Counter
Counter[x] == Counter[y]
3sorted[x] == sorted[y]
03031sorted[x] == sorted[y]
2from collections import Counter
Counter[x] == Counter[y]
0 from collections import Counter
Counter[x] == Counter[y]
1from collections import Counter
Counter[x] == Counter[y]
2223from collections import Counter
Counter[x] == Counter[y]
4from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
6from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
8from collections import Counter
Counter[x] == Counter[y]
3sorted[x] == sorted[y]
03031import 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]
6import 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
0import 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
76set[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 identical4
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]
6The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical7
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical8
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical9
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical0
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical4
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]
6The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical4
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical8
Đầ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
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.set[x] == set[y] # prefer this if elements are hashable
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
9from collections import Counter
Counter[x] == Counter[y]
0 from collections import Counter
Counter[x] == Counter[y]
1from collections import Counter
Counter[x] == Counter[y]
2223from collections import Counter
Counter[x] == Counter[y]
4from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
6from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
8from collections import Counter
Counter[x] == Counter[y]
3sorted[x] == sorted[y]
03031set[x] == set[y] # prefer this if elements are hashable
9from collections import Counter
Counter[x] == Counter[y]
0 from collections import Counter
Counter[x] == Counter[y]
1from collections import Counter
Counter[x] == Counter[y]
2223from collections import Counter
Counter[x] == Counter[y]
4from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
6from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
8from collections import Counter
Counter[x] == Counter[y]
3sorted[x] == sorted[y]
03031sorted[x] == sorted[y]
2from collections import Counter
Counter[x] == Counter[y]
0 from collections import Counter
Counter[x] == Counter[y]
1from collections import Counter
Counter[x] == Counter[y]
2223from collections import Counter
Counter[x] == Counter[y]
4from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
6from collections import Counter
Counter[x] == Counter[y]
3from collections import Counter
Counter[x] == Counter[y]
8from collections import Counter
Counter[x] == Counter[y]
3sorted[x] == sorted[y]
03031import 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]
6import 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
0import 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
76set[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 identical4
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]
6The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical7
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical8
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical9
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical0
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical4
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]
6The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical4
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical8
Đầu ra:
The first list is : [1, 2, 4, 3, 5] The second list is : [1, 2, 4, 3, 5] The lists are identical