Python equivalent of sum() using xor()

喜欢而已 提交于 2019-11-30 18:43:01
zxor = reduce(lambda a, b: a ^ b, z, 0)

import operator
zxor = reduce(operator.xor, z, 0)

Note that starting Python 3.8, and the introduction of assignment expressions (PEP 572) (:= operator), we can use and update a variable within a list comprehension and thus reduce a list to the xor of its elements:

zxor = 0
[zxor := zxor ^ x for x in [1, 0, 1, 0, 1, 0]]
# zxor = 1
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!