Python equivalent of sum() using xor()
问题 I like the Python sum function : >>> z = [1] * 11 >>> zsum = sum(z) >>> zsum == 11 True I want the same functionality with using xor (^) not add (+). I want to use map. But I can not work out how to do this. Any hints? I am not satisfied with this : def xor(l): r = 0 for v in l: r ^= v return v I want a 1 liner using map. Hints? 回答1: zxor = reduce(lambda a, b: a ^ b, z, 0) import operator zxor = reduce(operator.xor, z, 0) 回答2: Note that starting Python 3.8 , and the introduction of assignment