使用 Python 中的 Lambda 表达式和 Reduce 函数查找出现奇数次的数字
pythonserver side programmingprogramming更新于 2026/1/22 18:52:17
在本文中,我们需要从列表中找到在给定列表中出现奇数次的数字。我们还需要使用 Lambda 函数和 Reduce 函数。
我们设计了一个函数,其中 Reduce 函数用于应用 Lambda 函数来检查元素是否出现奇数次。
示例
from functools import reduce
def oddcount(i):
print(reduce(lambda x, y: x ^ y, i))
listA = [12,34,12,12,34]
print("给定列表:\n",listA)
print("The element present odd number of times:")
oddcount(listA)
输出
运行上述代码得到以下结果 −
给定列表: [12, 34, 12, 12, 34] The element present odd number of times: 12
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

