TypeError:^:'list'和'list'的不支持的操作数类型

时间:2016-03-10 10:36:02

标签: python operators

我正在阅读Learning Python by Mark Lutz。  它用 Python Expression Operators 章节编写:

  

x ^ y - 按位异或,设置对称差异

关于主题的brief googling后{I}期待symmetric difference的输出来自:{/ p>

[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 个答案:

答案 0 :(得分:3)

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

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