Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
The above functions can be combined to perform this task. We can employ reduce() to accumulate the result of XOR logic specified by the lambda function. Works only with Python2.
Đôi khi, trong khi lập trình, chúng tôi có một vấn đề trong đó chúng tôi có thể cần thực hiện các hoạt động bitwise nhất định giữa các yếu tố danh sách. Đây là một tiện ích thiết yếu khi chúng ta bắt gặp các hoạt động bitwise nhiều lần. Hãy để thảo luận về những cách nhất định trong đó XOR có thể được thực hiện.
Phương pháp số 1: Sử dụng The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 1 Các chức năng trên có thể được kết hợp để thực hiện nhiệm vụ này. Chúng ta có thể sử dụng giảm () để tích lũy kết quả của logic XOR được chỉ định bởi hàm Lambda. Chỉ hoạt động với Python2.
The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 2The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 3 The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 4The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 5The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 6The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 7The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 6The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 9The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 6>>> from functools import reduce >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> reduce(xor, map(int, bits)) 1 1__16
>>> from functools import reduce >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> reduce(xor, map(int, bits)) 1 7>>> from functools import reduce >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> reduce(xor, map(int, bits)) 1 8>>> from functools import reduce >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> reduce(xor, map(int, bits)) 1 9 >>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 0 >>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 1>>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 2
>>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 3The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 3 >>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 5>>> from functools import reduce >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> reduce(xor, map(int, bits)) 1 8>>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 7 >>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 8
The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2>>> from functools import reduce
>>> from operator import xor
>>> bits = ('0', '1', '0', '1', '0', '1', '0')
>>> reduce(xor, map(int, bits))
1
7>>> from functools import reduce
>>> from operator import xor
>>> bits = ('0', '1', '0', '1', '0', '1', '0')
>>> reduce(xor, map(int, bits))
1
8 x | y | x ^ y
---|---|------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 01 >>> from itertools import accumulate
>>> from operator import xor
>>> bits = ('0', '1', '0', '1', '0', '1', '0')
>>> list(accumulate(map(int, bits), xor))
[0, 1, 1, 0, 0, 1, 1]
0 >>> from itertools import accumulate
>>> from operator import xor
>>> bits = ('0', '1', '0', '1', '0', '1', '0')
>>> list(accumulate(map(int, bits), xor))
[0, 1, 1, 0, 0, 1, 1]
1 x | y | x ^ y
---|---|------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 04
This task can also be performed using this
method. In this the task performed by lambda function in above method is performed using ior function for cumulative XOR operation. Works with Python2 only.
Đầu ra:
Đôi khi, trong khi lập trình, chúng tôi có một vấn đề trong đó chúng tôi có thể cần thực hiện các hoạt động bitwise nhất định giữa các yếu tố danh sách. Đây là một tiện ích thiết yếu khi chúng ta bắt gặp các hoạt động bitwise nhiều lần. Hãy để thảo luận về những cách nhất định trong đó XOR có thể được thực hiện.
Phương pháp số 1: Sử dụng The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 1 Các chức năng trên có thể được kết hợp để thực hiện nhiệm vụ này. Chúng ta có thể sử dụng giảm () để tích lũy kết quả của logic XOR được chỉ định bởi hàm Lambda. Chỉ hoạt động với Python2.
The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 2The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 3 The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 4The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 5The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 6The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 7The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 6The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 9The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 6>>> from functools import reduce >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> reduce(xor, map(int, bits)) 1 1__16
>>> from functools import reduce >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> reduce(xor, map(int, bits)) 1 7>>> from functools import reduce >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> reduce(xor, map(int, bits)) 1 8>>> from functools import reduce >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> reduce(xor, map(int, bits)) 1 9 >>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 0 >>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 1>>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 2
>>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 3The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 3 >>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 5>>> from functools import reduce >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> reduce(xor, map(int, bits)) 1 8>>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 7 >>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1] 8
The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2Trong Python 3 bạn có thể sử dụng:
>>> from functools import reduce >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> reduce(xor, map(int, bits)) 1Hoặc nếu bạn muốn chạy XOR:
>>> from itertools import accumulate >>> from operator import xor >>> bits = ('0', '1', '0', '1', '0', '1', '0') >>> list(accumulate(map(int, bits), xor)) [0, 1, 1, 0, 0, 1, 1]Trong hướng dẫn này, chúng ta biết về hoạt động XOR và thực hiện nó trong hai danh sách trong chương trình Python. XOR là viết tắt của độc quyền của người Viking hoặc. Đó là để nói, BIT kết quả đánh giá thành của 1 1 nếu chỉ chính xác một trong các bit được đặt. Đây là bảng sự thật của nó:
This is its Truth Table:
Hoạt động này được thực hiện giữa hai bit tương ứng của bất kỳ số nào.
Ví dụ: 23 ^ 25 = 14 trong nhị phân: 10111 ^ 11001 = 0111023 ^ 25 = 14
In Binary: 10111 ^ 11001 = 01110
Chương trình Python để thực hiện XOR trên hai danh sách
Ở đây hai danh sách có chứa các phần tử số nguyên để thực hiện BitWise XOR. Sử dụng mô -đun zip để sử dụng giá trị đồng thời từ danh sách. Tất cả các yếu tố nằm trong thập phân và sản lượng cũng nằm trong số thập phân. Càng ^ ^ đang sử dụng cho ‘độc quyền hoặc ở Python.
” ^ ” is using for ‘exclusive or’ in python.
Thực hiện trong Python:
list1 = [3,4,5,6,7] # values are in decimal list2 = [10,4,2,9,23] # values are in decimal result = list(a^b for a,b in zip(list1,list2)) print('XOR =',result)OUTPUT:
XOR = [9, 0, 7, 15, 16] # in decimalĐể thay đổi các giá trị từ nhị phân sang thập phân và ngược lại
Nhị phân đến thập phân
>>> Binary = '1010' >>> int(Binary,2) # return decimal value OUTPUT: 10Thập phân đến nhị phân
The original list is : [4, 6, 2, 3, 8, 9] The Bitwise XOR of list elements are : 2 0Cảm ơn bạn đã ghé thăm Codespeedy. Tôi hy vọng nó sẽ giúp bạn.
Cũng đọc:
- Hoạt động bitwise trong Python
- Chuyển đổi bát phân thành thập lục phân trong Python
- Chương trình Python để chuyển đổi Radian ở mức độ