Hướng dẫn how do you find the xor of a list in python? - làm thế nào để bạn tìm thấy xor của một danh sách trong python?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • 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
    
    2
    The 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
    
    4
    The original list is : [4, 6, 2, 3, 8, 9]
    The Bitwise XOR of list elements are : 2
    
    5
    The original list is : [4, 6, 2, 3, 8, 9]
    The Bitwise XOR of list elements are : 2
    
    6
    The original list is : [4, 6, 2, 3, 8, 9]
    The Bitwise XOR of list elements are : 2
    
    7
    The original list is : [4, 6, 2, 3, 8, 9]
    The Bitwise XOR of list elements are : 2
    
    6
    The original list is : [4, 6, 2, 3, 8, 9]
    The Bitwise XOR of list elements are : 2
    
    9
    The 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]
    
    3
    The 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 | 0
    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]
    
    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 | 0
    4

    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
    
    2
    The 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
    
    4
    The original list is : [4, 6, 2, 3, 8, 9]
    The Bitwise XOR of list elements are : 2
    
    5
    The original list is : [4, 6, 2, 3, 8, 9]
    The Bitwise XOR of list elements are : 2
    
    6
    The original list is : [4, 6, 2, 3, 8, 9]
    The Bitwise XOR of list elements are : 2
    
    7
    The original list is : [4, 6, 2, 3, 8, 9]
    The Bitwise XOR of list elements are : 2
    
    6
    The original list is : [4, 6, 2, 3, 8, 9]
    The Bitwise XOR of list elements are : 2
    
    9
    The 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]
    
    3
    The 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
    


    Trong 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))
    1
    

    Hoặ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:

             x | y | x ^ y  
            ---|---|------  
             0 | 0 | 0 
             0 | 1 | 1 
             1 | 0 | 1 
             1 | 1 | 0

    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

         10111
       ^ 11001
        =======
         01110 = 14

    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.

    SYNTAX:
    >>> a ^ b

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

    Thậ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
    
    0

    Cả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 độ

    Làm thế nào để bạn tìm thấy giá trị XOR trong Python?

    Làm thế nào để thực hiện XOR bitwise trong Python ?..
    # Khởi tạo hai số nguyên A và B A = 15 B = 32 # Tìm Xor của A và B bằng cách sử dụng ^ toán tử c = a ^ b # in in v giá trị xor (a, "xor", b, "=", c).
    15 XOR 32 = 47 ..

    Danh sách liên kết XOR trong Python là gì?

    Danh sách liên kết XOR là một loại cấu trúc dữ liệu được sử dụng trong lập trình máy tính.Nó tận dụng hoạt động của BitWise XOR để giảm các yêu cầu lưu trữ cho các danh sách được liên kết gấp đôi.a type of data structure used in computer programming. It takes advantage of the bitwise XOR operation to decrease storage requirements for doubly linked lists.

    Bạn có thể xor 2 danh sách python không?

    Chương trình Python để thực hiện XOR trên hai danh sách bằ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' trong Python.Using the zip module to use simultaneous value each from the list. All elements are in Decimal and output is also in Decimal. ” ^ ” is using for 'exclusive or' in python.

    Làm cách nào để tìm số XOR của tôi?

    Để tìm XOR của nhiều hơn hai số, biểu thị tất cả các số trong biểu diễn nhị phân, hãy thêm 0 trước khi cần thiết.Viết chúng như thế này.và như thế.Để tìm mỗi bit XOR chỉ tính số 1 trong các bit tương ứng.represent all numbers in binary representation, add 0's before if necessary. Write them like this. and so on. To find each bit of XOR just calculate number of 1's in the corresponding bits.