rojas - 8 months ago 34

Python Question

Given a dict:

`data = {'18': [3.89, 1.28], '20': [1.39, 3.15], '15': [1.42, 3.10]}`

I want to pick out items that clearly differ from the rest as in

`18`

`ALLOWED_DISCREPANCY`

`0.5`

The

`18`

`3.89`

`abs(3.89 - 1.4)`

`0.5`

Answer Source

Compute the mean of the values.

```
>>> from numpy import mean
>>> data = {'18': [3.89, 1.28], '20': [1.39, 3.15], '15': [1.42, 3.10]}
>>> avg = mean([x for sublist in data.values() for x in sublist])
>>> avg
2.3716666666666666
```

Set the threshold and build a new dictionary which maps the original keys to a list of values that match your constraint. Here's two examples:

```
>>> thresh = 0.5
>>> {k:[x for x in v if abs(x-avg) > thresh] for k, v in data.items()}
{'18': [3.89, 1.28], '15': [1.42, 3.1], '20': [1.39, 3.15]}
>>>
>>> thresh = 1
>>> {k:[x for x in v if abs(x-avg) > thresh] for k, v in data.items()}
{'18': [3.89, 1.28], '15': [], '20': []}
```