17.12.2012 Views

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

Programmation PYTHON - Zenk - Security - Repository

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

0<br />

>>> d<br />

defaultdict(, {'a': 0})<br />

Principaux modules, partie 2<br />

CHAPITRE 9<br />

Ce comportement permet de s’affranchir du code d’initialisation lorsque les dictionnaires<br />

sont utilisés pour des calculs sur des séries.<br />

Occurrences de lettres dans un texte<br />

>>> from string import lowercase<br />

>>> from collections import defaultdict<br />

>>> sentence = "Ceci est un texte. Banal, certes. Mais c'est un texte"<br />

>>> counter = defaultdict(int)<br />

>>> for car in sentence:<br />

... if car not in lowercase:<br />

... continue<br />

... counter[car] += 1<br />

...<br />

>>> for car in lowercase:<br />

... print('%s: %s' % (car, counter[car]))<br />

...<br />

a: 3<br />

b: 0<br />

c: 3<br />

d: 0<br />

e: 9<br />

f: 0<br />

g: 0<br />

h: 0<br />

i: 2<br />

j: 0<br />

k: 0<br />

l: 1<br />

m: 0<br />

n: 3<br />

o: 0<br />

p: 0<br />

q: 0<br />

r: 1<br />

s: 4<br />

t: 7<br />

u: 2<br />

v: 0<br />

w: 0<br />

x: 2<br />

y: 0<br />

z: 0<br />

283

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!