# 1A.1 - Histogramme et dictionnaire¶

Compter les occurences de nombres dans une liste est assez facile avec un dictionnaire et on peut l’écrire de plusieurs manières différentes.

```import random
num = [random.randint(0,10) for i in range(1000)]
```
```hist = {}
for i in num:
hist[i] = hist.get(i, 0) + 1
```
```hist
```
```{0: 81,
1: 90,
2: 104,
3: 85,
4: 87,
5: 85,
6: 114,
7: 87,
8: 87,
9: 96,
10: 84}
```

Mesurer le temps que cela prend :

```def count1(num):
hist = {}
for i in num:
hist[i] = hist.get(i, 0) + 1
return hist

%timeit count1(num)
```
```195 µs ± 44.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
```

Comparons avec une autre implémentation :

```def count2(num):
hist = {}
for i in num:
if i in hist:
hist[i] += 1
else:
hist[i] = 1
return hist

%timeit count2(num)
```
```102 µs ± 6.24 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
```

Une version plus rapide :

```from collections import Counter

def count3(num):
return Counter(num)

%timeit count3(num)
```
```44.4 µs ± 3.86 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
```

Une dernière avec cytoolz :

```from cytoolz import count, groupby
import cytoolz

def count4(num):
return list(cytoolz.map(lambda x: (x[0], count(x[1])),
groupby(lambda x:x, num).items()))

%timeit count4(num)
```
```106 µs ± 3.23 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
```