如何在python中反转numpy浮点数列表的列表的每个元素?

时间:2018-11-02 19:24:25

标签: python list numpy

我有一个列表列表,我想为每个元素x做1/x

我尝试过

[1/x for x in list]

我该如何进行?我以乘法为例

[x*10 for x in list]

但这只会使行数增加10,而不会乘以实际值。我在做什么错了?

数据

[[8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.098671875, 16.09259521484375, 24.296372680664064],
 [8.049411010742187, 15.993977661132812, 24.147247314453125],
 [7.6774951171875, 15.250462036132813, 23.08390319824219],
 [7.450130615234375, 14.684713745117188, 22.35098876953125],
 [6.54051025390625, 12.876199340820312, 19.765568237304688],
 [5.39010986328125, 10.824963989257812, 16.51649169921875],
 [4.979031677246094, 9.665416870117188, 14.883976440429688],
 [4.6164859008789065, 8.65063751220703, 13.361966552734373],
 [3.9221954345703125, 7.188418579101563, 11.591380615234375],
 [3.4298834228515624, 6.534327087402344, 10.445729370117188],
 [2.9979116821289065, 5.92670654296875, 9.218810424804687],
 [2.7159127807617187, 5.452456359863281, 8.101337585449219],
 [2.216434326171875, 4.654409942626954, 6.799687652587892],
 [2.111146240234375, 4.231194915771484, 6.159953155517577],
 [1.6629426574707031, 3.4381407165527342, 5.140887298583984],
 [1.2951142883300781, 2.8314198303222655, 4.534166412353516],
 [1.1690933990478516, 2.3221934509277347, 3.6382754516601565],
 [1.1690933990478516, 2.1871324157714844, 3.230146026611328],
 [0.8830496215820313, 1.901088638305664, 2.645755157470703],
 [0.7231619262695312, 1.4308435058593751, 2.175510025024414],
 [0.2225925636291504, 0.7668316841125488, 1.5114982032775877],
 [0.2225925636291504, 0.4066648483276367, 0.9712825393676758],
 [0.2225925636291504, 0.2225925636291504, 0.2225925636291504],
 [0.2225925636291504, 0.2225925636291504, 0.2225925636291504],
 [0.0, 0.0, 0.0]]

3 个答案:

答案 0 :(得分:2)

这个问题被标记为numpy时,我想建议一个专门设计用于计算numpy的{​​{1}}函数:numpy.reciprocal

1/x

非常简单,如文档所述:

  

计算1 / x

但是您还应该注意:

  

注意:   此函数不适用于整数。

     

由于Python处理整数除法的方式,对于绝对值大于1的整数参数,结果始终为零。对于整数零,结果为溢出。

答案 1 :(得分:1)

使用numpy进行逐点算术运算。

>>> import numpy as np
>>> lst = [[1, 2], [3, 4]]
>>> a = np.array(lst, dtype=float)
>>> a
>>> 
array([[1., 2.],
       [3., 4.]])
>>> 1/a
>>> 
array([[1.        , 0.5       ],
       [0.33333333, 0.25      ]])

如果必须使用列表,请使用嵌套列表理解。

>>> [[1/x for x in sub] for sub in lst]
>>> [[1.0, 0.5], [0.3333333333333333, 0.25]]

...或者如果您希望获得不需要的结果:

>>> [1/x for sub in lst for x in sub]
>>> [1.0, 0.5, 0.3333333333333333, 0.25]

...,请记住不要除以零。 ;)

答案 2 :(得分:0)

您可以使用numpy按元素划分。

import numpy as np

a = np.array([[8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.098671875, 16.09259521484375, 24.296372680664064],
     [8.049411010742187, 15.993977661132812, 24.147247314453125],
     [7.6774951171875, 15.250462036132813, 23.08390319824219],
     [7.450130615234375, 14.684713745117188, 22.35098876953125],
     [6.54051025390625, 12.876199340820312, 19.765568237304688],
     [5.39010986328125, 10.824963989257812, 16.51649169921875],
     [4.979031677246094, 9.665416870117188, 14.883976440429688],
     [4.6164859008789065, 8.65063751220703, 13.361966552734373],
     [3.9221954345703125, 7.188418579101563, 11.591380615234375],
     [3.4298834228515624, 6.534327087402344, 10.445729370117188],
     [2.9979116821289065, 5.92670654296875, 9.218810424804687],
     [2.7159127807617187, 5.452456359863281, 8.101337585449219],
     [2.216434326171875, 4.654409942626954, 6.799687652587892],
     [2.111146240234375, 4.231194915771484, 6.159953155517577],
     [1.6629426574707031, 3.4381407165527342, 5.140887298583984],
     [1.2951142883300781, 2.8314198303222655, 4.534166412353516],
     [1.1690933990478516, 2.3221934509277347, 3.6382754516601565],
     [1.1690933990478516, 2.1871324157714844, 3.230146026611328],
     [0.8830496215820313, 1.901088638305664, 2.645755157470703],
     [0.7231619262695312, 1.4308435058593751, 2.175510025024414],
     [0.2225925636291504, 0.7668316841125488, 1.5114982032775877],
     [0.2225925636291504, 0.4066648483276367, 0.9712825393676758],
     [0.2225925636291504, 0.2225925636291504, 0.2225925636291504],
     [0.2225925636291504, 0.2225925636291504, 0.2225925636291504],
     [0.0, 0.0, 0.0]])

a = 1/a

print(a)

输出:

[[0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12347704 0.06214038 0.04115841]
 [0.12423269 0.06252353 0.04141259]
 [0.13025082 0.06557178 0.04332023]
 [0.13422583 0.06809802 0.04474075]
 [0.15289327 0.07766267 0.05059303]
 [0.18552498 0.09237906 0.06054555]
 [0.20084227 0.10346165 0.06718635]
 [0.21661498 0.11559842 0.07483928]
 [0.25495925 0.13911266 0.086271  ]
 [0.2915551  0.15303795 0.0957329 ]
 [0.33356553 0.16872777 0.10847387]
 [0.36820034 0.18340358 0.12343641]
 [0.45117511 0.21485001 0.14706558]
 [0.47367633 0.23633986 0.1623389 ]
 [0.60134365 0.29085488 0.19451895]
 [0.77213263 0.3531797  0.22054771]
 [0.85536365 0.43062734 0.27485549]
 [0.85536365 0.45721969 0.30958353]
 [1.13243919 0.5260144  0.37796392]
 [1.38281616 0.69888845 0.45966233]
 [4.49251306 1.30406714 0.66159523]
 [4.49251306 2.45902739 1.02956654]
 [4.49251306 4.49251306 4.49251306]
 [4.49251306 4.49251306 4.49251306]
 [       inf        inf        inf]]