Notiuni specifice Python



ROSEdu Py4School
decembrie 2013
- Mihai Tabara -



=> Introducere si sintaxa                  

=> Operatii de baza                          

=> Programare orientata pe obiecte


Notiuni specifice Python

1. List comprehensions


# list comprehension
[
expresie for element in lista if conditionala ]


# metoda clasica
for element in lista: if conditionala: expresie



>>> [x*2 for x in range(3)]
[0, 2, 4]


# folosind bucla for:
>>> patrate_perfecte = []
    for x in range(10):
        patrate_perfecte.append(x**2)
>>> print patrate_perfecte
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
 
#  alternativ, folosind list comprehensions 
>>> pperfecte = [x**2 for x in range(10)]
>>> print pperfecte
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

2. Nested list comprehensions



>>> matrice = [
... [1, 2, 3, 4],
... [5, 6, 7, 8],
... [9, 10, 11, 12],
... [13, 14, 15, 16],
... [17, 18, 19, 20],
... ]
>>> matrice
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]]

Soluția 1 (fără ''list comprehension'')




>>>
transpusa = [] >>> for i in range(4): ... linie_transpusa = [] ... for linie in matrice: linie_transpusa.append(linie[i]) ... transpusa.append(linie_transpusa) ...
>>> transpusa [[1, 5, 9, 13, 17], [2, 6, 10, 14, 18], [3, 7, 11, 15, 19], [4, 8, 12, 16, 20]]

Soluția 2 (cu ''list comprehension'')


>>> transpusa = []
>>> for i in range(4):
...     transpusa.append([linie[i] for linie in matrice])
... 
>>> transpusa [[1, 5, 9, 13, 17], [2, 6, 10, 14, 18], [3, 7, 11, 15, 19], [4, 8, 12, 16, 20]]

Soluția 3 (cu ''nested list comprehension'')



>>>
transpusa = [[linie[i] for linie in matrice] for i in range(4)]
>>>
transpusa [[1, 5, 9, 13, 17], [2, 6, 10, 14, 18], [3, 7, 11, 15, 19], [4, 8, 12, 16, 20]]

3. Dictionary comprehensions

Exemplu - afișarea primelor 10 pătrate perfecte:


>>> d = {n: n**2 for n in range(10)}
>>> d
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

4. map()

>>> def patrat(x):
        return x**2 
>>> patrate_perfecte = map(patrat, range(10))
>>> print patrate_perfecte
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

5. lambda

>>> pperfecte = map(lambda x: x**2, range(10))
>>> print pperfecte
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

6. filter

>>> patrate = map(lambda x: x**2, range(10))
>>> lista = filter(lambda x: x > 10 and x < 50, patrate)
>>> print lista
[16, 25, 36, 49]

7. dir()


=> string(), type() ...

>>> l = []
>>> dir(l)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']


Made with Slides.com