gpt4 book ai didi

python - 如何反转 python 中 numpy float 列表的列表中的每个元素?

转载 作者:行者123 更新时间:2023-11-30 22:05:01 24 4
gpt4 key购买 nike

我有一个列表的列表,我想在其中对每个元素 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]]

最佳答案

由于这个问题被标记为numpy,我想我建议使用一个专门用于计算1/xnumpy函数:numpy.reciprocal

>>> a
array([[1., 2.],
[3., 4.]])

>>> np.reciprocal(a)
array([[1. , 0.5 ],
[0.33333333, 0.25 ]])

非常简单,如文档中所述:

Calculates 1/x

但您还应该注意:

Note: This function is not designed to work with integers.

For integer arguments with absolute value larger than 1 the result is always zero because of the way Python handles integer division. For integer zero the result is an overflow.

关于python - 如何反转 python 中 numpy float 列表的列表中的每个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53124717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com