如何将列表列表中的负数转换为0?

sir*_*iri 1 python numpy list

[[213, -197, 124, 163], [455, 96, 149, 175], [364, 210, 131, 154], [-22, 119, 243, 308]]

Run Code Online (Sandbox Code Playgroud)

如何将里面的负数全部转为0?

[[213, 0, 124, 163], [455, 96, 149, 175], [364, 210, 131, 154], [0, 119, 243, 308]]
Run Code Online (Sandbox Code Playgroud)

谢谢

Dav*_*ngh 5

利用 numpy 的向量化特性:

import numpy as np

arr = np.array([[213, -197, 124, 163], [455, 96, 149, 175], [364, 210, 131, 154], [-22, 119, 243, 308]])

arr[arr < 0] = 0
print(arr)
Run Code Online (Sandbox Code Playgroud)