本文介绍了哪个Python模块适合列表中的数据操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个x,y和z坐标序列,需要对其进行操作.它们位于三个元组的一个列表中,例如{(x1,y1,z1),(x2,y2,z2),...}.
I have a sequence of x, y and z -coordinates, which I need to manipulate. They are in one list of three tuples, like {(x1, y1, z1), (x2, y2, z2), ...}.
我需要加法,乘法和对数来处理我的数据.
I need addition, multiplication and logarithm to manipulate my data.
我想研究一个与Awk语言一样强大的模块.
I would like to study a module, which is as powerful as Awk -language.
推荐答案
如果需要很多数组操作,那么numpy是python中的最佳选择
If you need many array manipulation, then numpy is the best choice in python
>>> import numpy
>>> data = numpy.array([(2, 4, 8), (3, 6, 5), (7, 5, 2)])
>>> data
array([[2, 4, 8],
[3, 6, 5],
[7, 5, 2]])
>>> data.sum() # product of all elements
42
>>> data.sum(axis=1) # sum of elements in rows
array([14, 14, 14])
>>> data.sum(axis=0) # sum of elements in columns
array([12, 15, 15])
>>> numpy.product(data, axis=1) # product of elements in rows
array([64, 90, 70])
>>> numpy.product(data, axis=0) # product of elements in columns
array([ 42, 120, 80])
>>> numpy.product(data) # product of all elements
403200
或对数组进行元素明智的操作
or element wise operation with arrays
>>> x,y,z = map(numpy.array,[(2, 4, 8), (3, 6, 5), (7, 5, 2)])
>>> x
array([2, 4, 8])
>>> y
array([3, 6, 5])
>>> z
array([7, 5, 2])
>>> x*y
array([ 6, 24, 40])
>>> x*y*z
array([ 42, 120, 80])
>>> x+y+z
array([12, 15, 15])
明智的数学运算,例如
>>> numpy.log(data)
array([[ 0.69314718, 1.38629436, 2.07944154],
[ 1.09861229, 1.79175947, 1.60943791],
[ 1.94591015, 1.60943791, 0.69314718]])
>>> numpy.exp(x)
array([ 7.3890561 , 54.59815003, 2980.95798704])
这篇关于哪个Python模块适合列表中的数据操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!