2

I have a list of a list, where I want to do 1/x for every element x.

I tried

[1/x for x in list]

How can I proceed? I tried multiplying as an example

[x*10 for x in list]

But this only increasing the number of rows by 10, and not multiplying the actual values. What am I doing wrong?

Data

[[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]]
5
  • 1
    Is this actually a list of lists? If so, why? A NumPy array would make this operation much more convenient and efficient. Commented Nov 2, 2018 at 19:27
  • It's actually an output from another list comprehension: new = ([list(itertools.accumulate(x)) for x in old]) Commented Nov 2, 2018 at 19:30
  • Right. I was thinking about this wrong and in terms of a list. It's a simple np operation if I convert it. Thanks! Commented Nov 2, 2018 at 19:32
  • numpy.cumsum could have handled the job you used the list comprehension for, too. Commented Nov 2, 2018 at 19:34
  • If alist is a lists of lists, then x in [x for x in alist] is itself a list. [1]*10 replicates the list; it doesn't multiply (in the numeric sense). Commented Nov 2, 2018 at 20:19

3 Answers 3

3

As this question is tagged numpy, I thought I'd suggest a numpy function designed specifically for calculating 1/x: numpy.reciprocal

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

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

Pretty simple, as stated in the docs:

Calculates 1/x

But you should also note:

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.

Sign up to request clarification or add additional context in comments.

1 Comment

With a numpy array a, you can also just write 1/a. If you are using Python 2 and a might be an integer array, you can write 1.0/a to ensure that the result is floating point.
1

Use numpy for pointwise arithmetic operations.

>>> 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      ]])

If you must use a list, use a nested list comprehension.

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

... or in case you want an un-nested result:

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

... and remember to not divide by zero. ;)

Comments

0

You can do it with numpy element-wise dividing.

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)

Output:

[[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]]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.