Skip to content
Snippets Groups Projects
Select Git revision
  • 5cba93d25eb47a677c14a56ef90a1e6176d544c2
  • master default protected
2 results

graph-parcours-dijkstra.py

Blame
  • comprehension.py 779 B
    S = [x**2 for x in range(10)]
    V = [2**i for i in range(13)]
    M = [x for x in S if x % 2 == 0]
    
    print(S); print(V); print(M)
    # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    # [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]
    # [0, 4, 16, 36, 64]
    
    
    S = {x**2 for x in range(10)}           # {} au lieu de [] : on veut un ensemble
    print(S)
    # {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}
    type(S)
    #set
    
    V = (2**i for i in range(13))
    print(V)
    # <generator object <genexpr> at 0x7f7bb55edc18>
    
    type(V)
    # generator
    
    # On peut mieux comprendre les listes en compréhension à travers leurs fonctions équivalentes
    S = []
    for i in range(10):
        S.append(i*2)
    
    # Et 
    S = [i*2 for i in range(10)]
    
    S
    # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
    
    [c for c in "foobar"]
    
    # Donne ['f', 'o', 'o', 'b', 'a', 'r']