Skip to main content
 首页 » 编程设计

python之在二维列表中交换 0 和 1 的最快方法

2025年05月04日12xxx_UU

假设我有一个矩阵:

[[1, 1, 1, 0, 0,], [0, 1, 0, 1], [1, 0, 1]] 

我想将其更改为:

[[0, 0, 0, 1, 1,], [1, 0, 1, 0], [0, 1, 0]] 

处理这种情况最快的方法是什么?

目前,我在另一个for循环中使用for循环,如下所示,这显然太慢了。

for my_row in my_mat: 
   for my_val in my_row: 
      my_val = 1 if my_val == 0 else 0 

请您参考如下方法:

我不认为它很慢,但它不是最快的。这里有一些更快的替代方案。

减法( super 简单)

>>> [[1 - j for j in i] for i in lst] 
[[0, 0, 0, 1, 1], [1, 0, 1, 0], [0, 1, 0]] 

这消除了对 if 检查的需要。不过,只有当您有一个 0/​​1 列表并且您只想翻转这些值时,这才有意义。


异或位翻转

>>> [[j ^ 1 for j in i] for i in lst] 
[[0, 0, 0, 1, 1], [1, 0, 1, 0], [0, 1, 0]] 

XOR 运算通常很快,因此如果您的正值超过 1,这是一个很好的选择。


不反转

>>> [[int(not j) for j in i] for i in lst] 
[[0, 0, 0, 1, 1], [1, 0, 1, 0], [0, 1, 0]] 

请注意,这会将非零值压缩为 1。not 将整数转换为 truthy/falsy 值,随后的 int 将 False 为 0,或 True 为 1。


如果您对此处建议的所有方法的性能感兴趣,

lst = np.random.choice(2, (1000, 1000)).tolist() 
 
%timeit [[int(not j) for j in i] for i in lst] 
%timeit [[j ^ 1 for j in i] for i in lst] 
%timeit [[1 - j for j in i] for i in lst] 
 
10 loops, best of 3: 175 ms per loop 
10 loops, best of 3: 89.8 ms per loop 
10 loops, best of 3: 61.1 ms per loop