从python中2D列表的所有元素中减去一个值 [英] Subtracting a value from all elements of a 2D list in python
本文介绍了从python中2D列表的所有元素中减去一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个2D列表,我想从2D列表的所有元素中减去一个浮点值:
I have a 2D list and I want to subtract a floating point value from all the elements of the 2D list:
mat = [[1, 2], [3, 4]]
cons = 13
mat1 = cons - mat
但是最后一行不起作用.如何在python的2D列表中执行此操作?
But the last line doesn't work. How to do this in a 2D list in python?
谢谢.
推荐答案
如果您做很多这样的事情,请考虑使用numpy
:
If you do a lot of such things, consider using numpy
:
In [1]: import numpy as np
In [2]: mat = np.array([[1, 2], [3, 4]])
In [3]: cons = 13
In [4]: cons-mat
Out[4]:
array([[12, 11],
[10, 9]])
这篇关于从python中2D列表的所有元素中减去一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文