Atirag - 6 months ago 41

Python Question

How can in python take a lists of lists and apply a division to the third element of each sublist.

The sublists looks like this

`[1.3735876284e-05, 0.9277849431216683, 34.02875434027778, 0.0]`

[2.60440773e-06, 7.35174234e-01, 2.79259180e+02, 0.00000000e+00]

...

I need to get the same sublists but the third element of each sublist (34.02 ..., 2.79 ...) should be divided by 100

Answer

You could try this:

```
a = [
[1.3735876284e-05, 0.9277849431216683, 34.02875434027778, 0.0],
[2.60440773e-06, 7.35174234e-01, 2.79259180e+02, 0.00000000e+00],
]
b = [
[(x / 100.0 if i == 2 else x) for (i, x) in enumerate(lst)]
for lst in a
]
```

Or the lambda version:

```
f = lambda a: [
[(x / 100.0 if i == 2 else x) for (i, x) in enumerate(lst)]
for lst in a
]
b = f(a)
```