您可以将字符转换为整数,然后将其转换为x或x:
l = [ord(a) ^ ord(b) for a,b in zip(s1,s2)]
如果由于XOR而需要一个字符串,这是一个更新的函数:
def sxor(s1,s2): # convert strings to a list of character pair tuples # go through each tuple, converting them to ASCII pre (ord) # perform exclusive or on the ASCII pre # then convert the result back to ASCIi (chr) # merge the resulting array of characters as a string return ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(s1,s2))
看到它在线运行:ideone



