[*] -  SecureInfo.eu, /_tmp/Python - Hints.md

Back


Ignore warnings

import warnings
warnings.filterwarnings("ignore")

zip

Créé un tableau associatif

for i in zip( [1,2,3] , 'cde' ):
  print(i)
(1, 'c')
(2, 'd')
(3, 'e')

map

Associe une fonction à un tableau

for i in map( ord , 'aze' ): print(i)
97
122
101

starmap

Associe une fonction à un tableau

from itertools import starmap
from hashlib import md5

def md5_string(data):
    return( md5(data.encode()).hexdigest() )

for i in starmap( md5_string , 'text' ):
    print(i)
e358efa489f58062f10dd7316b65649e
e1671797c52e15f763380b45e841ec32
9dd4e461268c8034f5c8564e155c67a6
e358efa489f58062f10dd7316b65649e

Sort dictionnary

from collections import OrderedDict

d1 = {'a': 1, 'b': 2, 'c': 3, 'd':4, 'a1':5, 'b1': 10, 'c1': 8, 'd1': 6}
OrderedDict( sorted( d1.items(), key=lambda t: t[1] ) )

Sortie :

OrderedDict([('a', 1),
             ('b', 2),
             ('c', 3),
             ('d', 4),
             ('a1', 5),
             ('d1', 6),
             ('c1', 8),
             ('b1', 10)])
OrderedDict( sorted( d1.items(), key=lambda t: t[0] ) )

Sortie :

OrderedDict([('a', 1),
             ('a1', 5),
             ('b', 2),
             ('b1', 10),
             ('c', 3),
             ('c1', 8),
             ('d', 4),
             ('d1', 6)])
⬆️ Top
admin