在 Python 中,我们可以在列表、元组、集合、字典等数据结构上使用按位运算符吗?如果是这样,为什么?

In Python can we use bitwise operators on data structures such as lists, tuples, sets, dictionaries? And if so, why?

现在我明白这些数据结构中的数据必须是整数类型才能可行,但它是如何工作的?

假设我有一个列表列表或其中包含元组的集合;结果会是什么样子,逻辑上意味着什么?

list_a = [[1,34,24],[12,727,2]]
list_b =[[12,727,2]]

some_list = list_a & list_b
# what would the above list look like?

set_1 = {(2,3),(3,4),(4,5)}

set_2 = {(1,3),(2,5),(6,7),(1,0)}

some_set = set_1 | set2
# what would the above set look like?

我可以在生成的数据结构上使用逻辑运算符吗?

if some_value in set1 | set2:
    # do something

它们本身不是 按位 运算符。它们是 运算符 ,并且每种类型都可以自行定义将对它们执行的操作。 &| 运算符映射到 __and__ and __or__ methods of an object respectively. Sets define operations for these (intersection and union respectively), while lists do not。尽管列表为 + 定义了一个操作(列表串联)。

Sooo…set_1 | set_2 结果:

{(2, 3), (6, 7), (4, 5), (3, 4), (1, 0), (2, 5), (1, 3)}

至于问题的其余部分:Mu