Python – 列表元素间的按位或运算


简介

Python 语言属于面向对象编程 (OOP) 概念,它会在不检查错误的情况下立即运行代码。这种语言由 Guido Rossum 于 1989 年发明,并于 1991 年在全球发布。Python 是一种用途广泛的高级语言,用户可以轻松理解。在当今世界,对于拥有大量数据的组织来说,数据处理是最具挑战性的任务之一,而随着数据科学和机器学习的发展,数据访问变得更加容易。

列表元素间的按位或运算

用于表示按位或运算符的运算符是“|”。按位或也可以使用 union() 函数定义为集合并集。按位或运算符的示例如下所示,其中包含两个变量:

A = [2, 4, 6, 9]
B = [3, 6, 7]

上述元素可以通过两种方式进行按位或运算:

情况 1

result= A|B

情况 2

result = B.union(A)

方法

方法 1 - 使用迭代方法

方法 2 - 使用 lambda 方法

方法 3 - 使用 numpy 模块

方法 1:使用迭代方法进行 Python 列表元素间的按位或运算

算法

  • 步骤 1 - 使用三个整型元素 40、50 和 90 初始化列表数据结构。

  • 步骤 2 - 将 out 变量赋值为“0boo”,它在十进制中等于 0。

  • 步骤 3 - 列表的每个元素都执行按位或运算。

  • 步骤 4 - 将 out 的当前值和 list_1 的当前元素进行比较,并将结果存储在名为“out”的变量中。

  • 步骤 5 - 然后,print 语句将返回执行按位或运算符后的值。

示例

#initializing the list with three integer elements
list_1=[40,50,90]
#the output is assigned a "0" decimal value
out=0b00
#for loop is used to iterate through the list using range() and length of the list
for a in range(len(list_1)):
   #list returns bitwise OR operator of the list
   out|=list_1[a]

#it returns the value after performing bitwise OR operator
print("List after performing the operation",out)

输出

List after performing the operation 122

方法 2:使用 lambda 函数进行 Python 列表元素间的按位或运算

算法

  • 步骤 1 - 导入所需的模块以使用 reduce() 函数。

  • 步骤 2 - 使用三个整型元素 40、50 和 90 定义列表数据结构。

  • 步骤 3 - 使用符号“|”使用 lambda 函数执行按位或运算。

  • 步骤 4 - 为此,使用 key 参数,然后 lambda 函数使用 reduce 函数来获取结果。

  • 步骤 5 - 然后,print 语句将返回执行按位或运算符后的值,以二进制格式和十进制格式两种形式显示。

示例

#importing the functools module to use the reduce function
from functools import reduce

#initializing the list with three integer elements
list_1 = [40, 50, 90]

#lambda function is used to find the bitwise OR with the key parameter
#after generating the result, it is reduced using the reduce() function and stored in out variable
result = reduce(lambda a, b: a | b, list_1)
#it returns the value after performing bitwise OR operator
print("The Bitwise OR operator of the given list in binary:",bin(result))

print("The Bitwise OR operator of the given list :",result)

输出

The Bitwise OR operator of the given list in binary: 0b1111010
The Bitwise OR operator of the given list : 122

方法 3:使用 numpy 模块进行 Python 列表元素间的按位或运算

算法

  • 步骤 1 - 导入所需的模块以使用“np”函数。

  • 步骤 2 - 使用三个整型元素 40、50 和 90 初始化列表数据结构。

  • 步骤 3 - 使用名为“np.bitwise_or.reduce()”的函数对列表的所有元素执行按位或运算。

  • 步骤 4 - 然后,print 语句将返回执行按位或运算符后的值(十进制格式)。

#importing the numpy module to use the "np" function
import numpy as np

#initializing the list with three integer elements
list_1 = [40, 50, 90]

#direct function to add the elements of the list is done
result = np.bitwise_or.reduce(list_1)

#it returns the value after performing bitwise OR operator
print("List after performing the operation", result)

输出

List after performing the operation 122

结论

本文描述了三种执行按位或运算的方法。在第一种方法中,使用 for 循环和 range() 和 len() 方法迭代列表的元素。在第二种方法中,lambda 函数与 reduce() 函数一起使用。当函数未定义或未知时,lambda 函数可以高效地使用。在最后一种方法中,导入 numpy 模块以使用“np”函数执行按位或运算。

更新于:2023年8月25日

浏览量:354

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.