Hướng dẫn find unique elements in two lists python - tìm các phần tử duy nhất trong hai danh sách python

UPDATE:

Cảm ơn @ahito:

In : list(set(x).symmetric_difference(set(f)))

Out: [33, 2, 22, 11, 44]

Bài viết này có một sơ đồ gọn gàng giải thích những gì sự khác biệt đối xứng làm.

Câu trả lời cũ:

Sử dụng tài liệu của Python trên các bộ:

>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}

Tôi đã đưa ra đoạn mã này để có được các yếu tố duy nhất từ ​​hai danh sách:

(set(x) | set(f)) - (set(x) & set(f))

hoặc sửa đổi một chút để trả về list:

list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list

Here:

  1. Nhà điều hành
    >>> # Demonstrate set operations on unique letters from two words
    ...
    >>> a = set('abracadabra')
    >>> b = set('alacazam')
    >>> a                                  # unique letters in a
    {'a', 'r', 'b', 'c', 'd'}
    >>> a - b                              # letters in a but not in b
    {'r', 'd', 'b'}
    >>> a | b                              # letters in a or b or both
    {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
    >>> a & b                              # letters in both a and b
    {'a', 'c'}
    >>> a ^ b                              # letters in a or b but not both
    {'r', 'd', 'b', 'm', 'z', 'l'}
    
    0 trả về các phần tử trong
    >>> # Demonstrate set operations on unique letters from two words
    ...
    >>> a = set('abracadabra')
    >>> b = set('alacazam')
    >>> a                                  # unique letters in a
    {'a', 'r', 'b', 'c', 'd'}
    >>> a - b                              # letters in a but not in b
    {'r', 'd', 'b'}
    >>> a | b                              # letters in a or b or both
    {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
    >>> a & b                              # letters in both a and b
    {'a', 'c'}
    >>> a ^ b                              # letters in a or b but not both
    {'r', 'd', 'b', 'm', 'z', 'l'}
    
    1,
    >>> # Demonstrate set operations on unique letters from two words
    ...
    >>> a = set('abracadabra')
    >>> b = set('alacazam')
    >>> a                                  # unique letters in a
    {'a', 'r', 'b', 'c', 'd'}
    >>> a - b                              # letters in a but not in b
    {'r', 'd', 'b'}
    >>> a | b                              # letters in a or b or both
    {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
    >>> a & b                              # letters in both a and b
    {'a', 'c'}
    >>> a ^ b                              # letters in a or b but not both
    {'r', 'd', 'b', 'm', 'z', 'l'}
    
    2 hoặc cả haiboth
  2. Người vận hành
    >>> # Demonstrate set operations on unique letters from two words
    ...
    >>> a = set('abracadabra')
    >>> b = set('alacazam')
    >>> a                                  # unique letters in a
    {'a', 'r', 'b', 'c', 'd'}
    >>> a - b                              # letters in a but not in b
    {'r', 'd', 'b'}
    >>> a | b                              # letters in a or b or both
    {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
    >>> a & b                              # letters in both a and b
    {'a', 'c'}
    >>> a ^ b                              # letters in a or b but not both
    {'r', 'd', 'b', 'm', 'z', 'l'}
    
    3 trả về các phần tử trong cả
    >>> # Demonstrate set operations on unique letters from two words
    ...
    >>> a = set('abracadabra')
    >>> b = set('alacazam')
    >>> a                                  # unique letters in a
    {'a', 'r', 'b', 'c', 'd'}
    >>> a - b                              # letters in a but not in b
    {'r', 'd', 'b'}
    >>> a | b                              # letters in a or b or both
    {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
    >>> a & b                              # letters in both a and b
    {'a', 'c'}
    >>> a ^ b                              # letters in a or b but not both
    {'r', 'd', 'b', 'm', 'z', 'l'}
    
    1 và
    >>> # Demonstrate set operations on unique letters from two words
    ...
    >>> a = set('abracadabra')
    >>> b = set('alacazam')
    >>> a                                  # unique letters in a
    {'a', 'r', 'b', 'c', 'd'}
    >>> a - b                              # letters in a but not in b
    {'r', 'd', 'b'}
    >>> a | b                              # letters in a or b or both
    {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
    >>> a & b                              # letters in both a and b
    {'a', 'c'}
    >>> a ^ b                              # letters in a or b but not both
    {'r', 'd', 'b', 'm', 'z', 'l'}
    
    2both
    >>> # Demonstrate set operations on unique letters from two words
    ...
    >>> a = set('abracadabra')
    >>> b = set('alacazam')
    >>> a                                  # unique letters in a
    {'a', 'r', 'b', 'c', 'd'}
    >>> a - b                              # letters in a but not in b
    {'r', 'd', 'b'}
    >>> a | b                              # letters in a or b or both
    {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
    >>> a & b                              # letters in both a and b
    {'a', 'c'}
    >>> a ^ b                              # letters in a or b but not both
    {'r', 'd', 'b', 'm', 'z', 'l'}
    
    1 and
    >>> # Demonstrate set operations on unique letters from two words
    ...
    >>> a = set('abracadabra')
    >>> b = set('alacazam')
    >>> a                                  # unique letters in a
    {'a', 'r', 'b', 'c', 'd'}
    >>> a - b                              # letters in a but not in b
    {'r', 'd', 'b'}
    >>> a | b                              # letters in a or b or both
    {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
    >>> a & b                              # letters in both a and b
    {'a', 'c'}
    >>> a ^ b                              # letters in a or b but not both
    {'r', 'd', 'b', 'm', 'z', 'l'}
    
    2
  3. >>> # Demonstrate set operations on unique letters from two words
    ...
    >>> a = set('abracadabra')
    >>> b = set('alacazam')
    >>> a                                  # unique letters in a
    {'a', 'r', 'b', 'c', 'd'}
    >>> a - b                              # letters in a but not in b
    {'r', 'd', 'b'}
    >>> a | b                              # letters in a or b or both
    {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
    >>> a & b                              # letters in both a and b
    {'a', 'c'}
    >>> a ^ b                              # letters in a or b but not both
    {'r', 'd', 'b', 'm', 'z', 'l'}
    
    6 Người vận hành trừ kết quả của
    >>> # Demonstrate set operations on unique letters from two words
    ...
    >>> a = set('abracadabra')
    >>> b = set('alacazam')
    >>> a                                  # unique letters in a
    {'a', 'r', 'b', 'c', 'd'}
    >>> a - b                              # letters in a but not in b
    {'r', 'd', 'b'}
    >>> a | b                              # letters in a or b or both
    {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
    >>> a & b                              # letters in both a and b
    {'a', 'c'}
    >>> a ^ b                              # letters in a or b but not both
    {'r', 'd', 'b', 'm', 'z', 'l'}
    
    3 từ
    >>> # Demonstrate set operations on unique letters from two words
    ...
    >>> a = set('abracadabra')
    >>> b = set('alacazam')
    >>> a                                  # unique letters in a
    {'a', 'r', 'b', 'c', 'd'}
    >>> a - b                              # letters in a but not in b
    {'r', 'd', 'b'}
    >>> a | b                              # letters in a or b or both
    {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
    >>> a & b                              # letters in both a and b
    {'a', 'c'}
    >>> a ^ b                              # letters in a or b but not both
    {'r', 'd', 'b', 'm', 'z', 'l'}
    
    0 và cung cấp cho chúng tôi các yếu tố chỉ được trình bày duy nhất trong một trong các danh sách

Đưa ra một danh sách, in tất cả các số duy nhất theo bất kỳ thứ tự nào.

Examples:  

Đầu vào: 10 20 10 30 40 40 40OUTPUT: 10 20 30 40 & nbsp;10 20 10 30 40 40
Output : 10 20 30 40 

Đầu vào: 1 2 1 1 3 4 3 3 5 & nbsp; đầu ra: 1 2 3 4 5 & nbsp;1 2 1 1 3 4 3 3 5 
Output : 1 2 3 4 5  

Phương pháp 1: Truyền tải danh sách

Sử dụng Traversal, chúng ta có thể đi qua mọi yếu tố trong danh sách và kiểm tra xem phần tử có nằm trong độc đáo không nếu nó không ở đó, thì chúng ta có thể nối nó vào độc đáo. Điều này được thực hiện bằng cách sử dụng một vòng cho vòng lặp và một câu lệnh IF khác kiểm tra xem giá trị có nằm trong danh sách duy nhất hay không tương đương với một câu nói khác cho một vòng lặp. & NBSP; & nbsp;

Python

>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
9
(set(x) | set(f)) - (set(x) & set(f))
0

(set(x) | set(f)) - (set(x) & set(f))
1
(set(x) | set(f)) - (set(x) & set(f))
223
(set(x) | set(f)) - (set(x) & set(f))
4

(set(x) | set(f)) - (set(x) & set(f))
1
(set(x) | set(f)) - (set(x) & set(f))
6
(set(x) | set(f)) - (set(x) & set(f))
7
(set(x) | set(f)) - (set(x) & set(f))
8
(set(x) | set(f)) - (set(x) & set(f))
9

list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list
0
list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list
1
(set(x) | set(f)) - (set(x) & set(f))
7
list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list
3
(set(x) | set(f)) - (set(x) & set(f))
8
list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list
5

list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list
6
list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list
7

(set(x) | set(f)) - (set(x) & set(f))
1
(set(x) | set(f)) - (set(x) & set(f))
6
(set(x) | set(f)) - (set(x) & set(f))
7
(set(x) | set(f)) - (set(x) & set(f))
8
list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list
5

list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list
0
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
5

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
6
(set(x) | set(f)) - (set(x) & set(f))
3
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
8
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
1
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
5
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
050575750505757560

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
2
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
3
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
4

the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
5

the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
6
(set(x) | set(f)) - (set(x) & set(f))
3
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
8
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
10 20 30 40

the unique values from 2nd list is
1 2 3 4 5
1
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
05069
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0______69
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0__

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
2
the unique values from 1st list is
[10, 20, 30, 40]

the unique values from 2nd list is
[1, 2, 3, 4, 5]
9
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
4

list1

Đầu ra: & nbsp; 

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5

Hướng dẫn find unique elements in two lists python - tìm các phần tử duy nhất trong hai danh sách python

Phương pháp 2: Sử dụng tập hợp

Sử dụng thuộc tính Set () của Python, chúng ta có thể dễ dàng kiểm tra các giá trị duy nhất. Chèn các giá trị của danh sách trong một tập hợp. Đặt chỉ lưu trữ một giá trị một lần ngay cả khi nó được chèn nhiều lần. Sau khi chèn tất cả các giá trị trong tập hợp bởi list_set = set (list1), hãy chuyển đổi bộ này thành một danh sách để in nó. & Nbsp;

Python

>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
9
(set(x) | set(f)) - (set(x) & set(f))
0

(set(x) | set(f)) - (set(x) & set(f))
1
(set(x) | set(f)) - (set(x) & set(f))
223
(set(x) | set(f)) - (set(x) & set(f))
4

(set(x) | set(f)) - (set(x) & set(f))
1
(set(x) | set(f)) - (set(x) & set(f))
6
(set(x) | set(f)) - (set(x) & set(f))
7
(set(x) | set(f)) - (set(x) & set(f))
8
(set(x) | set(f)) - (set(x) & set(f))
9

(set(x) | set(f)) - (set(x) & set(f))
1
(set(x) | set(f)) - (set(x) & set(f))
6
(set(x) | set(f)) - (set(x) & set(f))
7
(set(x) | set(f)) - (set(x) & set(f))
8
list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list
5

list((set(x) | set(f)) - (set(x) & set(f))) #if you need a list
0
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
5

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
6
(set(x) | set(f)) - (set(x) & set(f))
3
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
8
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
1
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
5
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
050575750505757560

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
2
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
3
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
4

the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
5

the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
6
(set(x) | set(f)) - (set(x) & set(f))
3
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
8
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
10 20 30 40

the unique values from 2nd list is
1 2 3 4 5
1
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
05069
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0______69
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0__

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
2
the unique values from 1st list is
[10, 20, 30, 40]

the unique values from 2nd list is
[1, 2, 3, 4, 5]
9
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
4

list1

Đầu ra: & nbsp;

the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5

Phương pháp 2: Sử dụng tập hợp

Sử dụng thuộc tính Set () của Python, chúng ta có thể dễ dàng kiểm tra các giá trị duy nhất. Chèn các giá trị của danh sách trong một tập hợp. Đặt chỉ lưu trữ một giá trị một lần ngay cả khi nó được chèn nhiều lần. Sau khi chèn tất cả các giá trị trong tập hợp bởi list_set = set (list1), hãy chuyển đổi bộ này thành một danh sách để in nó. & Nbsp;x=numpy.array(list) and then use numpy.unique(x) function to get the unique values from the list. numpy.unique() returns only the unique values in the list. 

Python3

(set(x) | set(f)) - (set(x) & set(f))
1list5
(set(x) | set(f)) - (set(x) & set(f))
3 list7list8

>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
9
(set(x) | set(f)) - (set(x) & set(f))
0

(set(x) | set(f)) - (set(x) & set(f))
1
(set(x) | set(f)) - (set(x) & set(f))
223
(set(x) | set(f)) - (set(x) & set(f))
4

(set(x) | set(f)) - (set(x) & set(f))
1
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
69

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
6
(set(x) | set(f)) - (set(x) & set(f))
3
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
8
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
1
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
5
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
050575750505757560

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
2
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
3
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
4

the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
5

the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
6
(set(x) | set(f)) - (set(x) & set(f))
3
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
8
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
10 20 30 40

the unique values from 2nd list is
1 2 3 4 5
1
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
05069
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0______69
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0__

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
2
the unique values from 1st list is
[10, 20, 30, 40]

the unique values from 2nd list is
[1, 2, 3, 4, 5]
9
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
4

list1

Output: 

Đầu ra: & nbsp;

Phương pháp 2: Sử dụng tập hợp

Sử dụng bộ đếm nhập Python () từ các bộ sưu tập in tất cả các khóa của các phần tử bộ đếm hoặc chúng tôi in trực tiếp bằng cách sử dụng biểu tượng***. Dưới đây là việc thực hiện phương pháp trên.“*” symbol. Below is the implementation of above approach.

Python3

(set(x) | set(f)) - (set(x) & set(f))
16
(set(x) | set(f)) - (set(x) & set(f))
17
>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
59
(set(x) | set(f)) - (set(x) & set(f))
19

>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
9
(set(x) | set(f)) - (set(x) & set(f))
0

(set(x) | set(f)) - (set(x) & set(f))
1
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
2
(set(x) | set(f)) - (set(x) & set(f))
25
(set(x) | set(f)) - (set(x) & set(f))
26

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
6
(set(x) | set(f)) - (set(x) & set(f))
3
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
8
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
1
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
5
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
050575750505757560

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
2
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
3
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
4

the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
5

the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
6
(set(x) | set(f)) - (set(x) & set(f))
3
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
8
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
10 20 30 40

the unique values from 2nd list is
1 2 3 4 5
1
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
05069
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0______69
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0__

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
2
the unique values from 1st list is
[10, 20, 30, 40]

the unique values from 2nd list is
[1, 2, 3, 4, 5]
9
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
4

list1

Đầu ra

the unique values from 1st list is
10 20 30 40

the unique values from 2nd list is
1 2 3 4 5

Phương pháp số 4: Sử dụng giảm ()

Sử dụng python nhập giảm () từ functools và lặp lại trên tất cả các phần tử và kiểm tra xem phần tử là giá trị trùng lặp hoặc duy nhất. Dưới đây là việc thực hiện phương pháp trên.

Python

(set(x) | set(f)) - (set(x) & set(f))
16
(set(x) | set(f)) - (set(x) & set(f))
74
>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
59
(set(x) | set(f)) - (set(x) & set(f))
76

>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
9
(set(x) | set(f)) - (set(x) & set(f))
0

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
6
(set(x) | set(f)) - (set(x) & set(f))
3
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
8
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
1
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
5
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
050575750505757560

the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
6
(set(x) | set(f)) - (set(x) & set(f))
3
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
8
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
10 20 30 40

the unique values from 2nd list is
1 2 3 4 5
1
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
05069
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0______69
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0__

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
6
(set(x) | set(f)) - (set(x) & set(f))
3
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
8
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
1
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
5
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
050575750505757560

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
2
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
3
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
4

the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
5

the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
6
(set(x) | set(f)) - (set(x) & set(f))
3
the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
8
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
9
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0
the unique values from 1st list is
10 20 30 40

the unique values from 2nd list is
1 2 3 4 5
1
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
05069
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0______69
the unique values from 1st list is
40 10 20 30 
the unique values from 2nd list is
1 2 3 4 5
0__

the unique values from 1st list is
10 20 30 40 
the unique values from 2nd list is
1 2 3 4 5
4
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
2
the unique values from 1st list is
[10, 20, 30, 40]

the unique values from 2nd list is
[1, 2, 3, 4, 5]
9
the unique values from 1st list is
[10 20 30 40]

the unique values from 2nd list is
[1 2 3 4 5]
4

list1

Đầu ra

the unique values from 1st list is
[10, 20, 30, 40]

the unique values from 2nd list is
[1, 2, 3, 4, 5]


Làm cách nào để nhận được các yếu tố độc đáo từ hai danh sách trong Python?

Một trong những cách sau đây có thể được sử dụng để có được các giá trị duy nhất từ ​​danh sách trong Python:..
Phương thức python set () ..
Sử dụng danh sách Python. Phương thức nối () cùng với một vòng lặp ..
Sử dụng Python Numpy. Phương thức duy nhất () ..

Làm thế nào để tôi tìm thấy các giá trị duy nhất giữa hai danh sách?

Tìm các giá trị duy nhất/trùng lặp giữa hai cột với công thức.Công thức sau đây cũng có thể giúp bạn tìm các giá trị duy nhất, vui lòng thực hiện như thế này: Trong một ô trống B2, hãy nhập công thức này = if (isna (vlookup (A2, $ c $ 2: $ c $ 13,1, sai)),"Có", ""), và sau đó kéo tay cầm tự động của ô này vào ô B15.=IF(ISNA(VLOOKUP(A2,$C$2:$C$13,1,FALSE)),"Yes",""), and then drag this cell's AutoFill Handle to the cell B15.

Làm thế nào để bạn có được các yếu tố không phổ biến từ hai bộ trong Python?

Phương pháp hiệu quả và được khuyến nghị nhất để thực hiện nhiệm vụ này là sử dụng kết hợp của set () và map () để đạt được nó.Đầu tiên chuyển đổi danh sách bên trong thành các bộ dữ liệu bằng MAP và Danh sách bên ngoài để đặt, việc sử dụng ^ toán tử có thể thực hiện sự khác biệt đối xứng đã đặt và do đó thực hiện nhiệm vụ này.using the combination of set() and map() to achieve it. Firstly converting inner lists to tuples using map, and outer lists to set, use of ^ operator can perform the set symmetric difference and hence perform this task.

Làm thế nào để bạn tìm thấy các mục phổ biến trong hai danh sách trong Python?

Sử dụng hàm giao lộ () Đây là phương pháp dễ nhất để tìm các yếu tố phổ biến trong hai danh sách trong Python.Như tên cho thấy, hàm giao lộ () là hàm python tích hợp được sử dụng để trả về một tập hợp chứa các phần tử phổ biến trong hai bộ. This is the easiest method to find common elements in two lists in Python. As the name suggests, the intersection() function is a built-in python function that is used to return a set that contains the elements which are common in two sets.