2016-03-10 93 views
0

我在阅读Learning Python by Mark Lutz。 它在Python表达式运算符书面章:TypeError:^:'list'和'list'的不受支持的操作数类型

x^y - Bitwise XOR, set symmetric difference

brief googling后的主题是什么symmetric difference我希望[1, 3]从输出:

y = ['1', '2'] 
x = ['2', '3'] 
print x^y 

而是我:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: unsupported operand type(s) for ^: 'list' and 'list' 

我没有得到什么? ^究竟是为了什么?

+1

将它与'set',而不是'list'一起使用... – deceze

回答

3

正如文档所述,它设置了对称差异,并且python使用set对象来演示支持所有集合操作的集合。

>>> y = {'1', '2'} 
>>> x = {'2', '3'} 
>>> 
>>> x^y 
set(['1', '3']) 
相关问题