Intro sur les listes en Python

In [3]:
Nom=["Alice","Bob","Chloé","David","Emma","Fred","Zoé"]
Nom[0]
Out[3]:
'Alice'
In [4]:
L1 = [0,0,1,1,0,0,1]
L2 = [0,0,0,0,1,1,0]
L3 = [1,0,0,1,1,1,0]
matrice = [L1,L2,L3]
matrice
Out[4]:
[[0, 0, 1, 1, 0, 0, 1], [0, 0, 0, 0, 1, 1, 0], [1, 0, 0, 1, 1, 1, 0]]
In [5]:
for ligne in matrice :
    print(ligne)
[0, 0, 1, 1, 0, 0, 1]
[0, 0, 0, 0, 1, 1, 0]
[1, 0, 0, 1, 1, 1, 0]
In [6]:
matrice[0]
Out[6]:
[0, 0, 1, 1, 0, 0, 1]
In [7]:
matrice[0] == L1
Out[7]:
True
In [8]:
matrice[0][2]   # L1[2]
Out[8]:
1

Exercice-Graphe-Réseaux sociaux

In [ ]:
# "Être ami" avec quelqu'un
friend=[[0,0,1,1,0,0,1],
        [0,0,0,0,1,1,0],
        [1,0,0,1,1,1,0],
        [1,0,1,0,0,0,1],
        [0,1,1,0,0,1,0],
        [0,1,1,0,1,0,0],
        [1,0,0,1,0,0,0]]

Nom=["Alice","Bob","Chloé","David","Emma","Fred","Zoé"]

for i in range(0,7,1):
    nombamis=0
    for j in range(0,7,1):
        if friend[i][j]==1:
            nombamis=nombamis+1
    print(Nom[i]," a ",nombamis, "amis")
In [ ]:
# "Suivre" quelqu'un
follow=[[0,0,1,0,0,0,1],
        [0,0,0,0,1,1,1],
        [1,0,0,1,0,1,1],
        [1,0,0,0,0,0,1],
        [0,0,0,1,0,1,1],
        [0,0,0,0,1,0,1],
        [0,0,0,0,0,0,0]]