1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| artiste = []
artiste.append({
'nom': "parachutes",
'date' : 2000,
'type' : "rock",
'chansons': [
{
'titre': 'dont panic',
'duree': 2.17
},
{
'titre': 'shiver',
'duree': 4.59
},
{
'titre': 'spies',
'duree': 5.18
},
]
})
artiste.append({
'nom': "Everyday Life",
'date': 2018,
'chansons': [
{
'titre': 'Sunrise',
'duree': 2.31
},
{
'titre': 'Church',
'duree': 3.50
},
]
})
print(*artiste, sep="\n")
def recherche(artiste, titre="", annee=0, duree_max=0, duree_mini=0, style=""):
# retourne un tableau de chansons avec détail
chansons = []
for album in artiste:
for chanson in album["chansons"]:
chanson["album"] = album["nom"]
chanson["date"] = album["date"]
chanson["type"] = album.get("type","?")
chansons.append(chanson)
# nous avons maintenant un tableau de chaque chansons avec détails
# print(chansons)
# filtres
if annee:
chansons = [c for c in chansons if c["date"] == annee]
if style:
chansons = [c for c in chansons if c["type"] == style]
if titre:
chansons = [c for c in chansons if c["titre"] == titre]
if duree_max:
chansons = [c for c in chansons if c["duree"] <= duree_max]
if duree_mini:
chansons = [c for c in chansons if c["duree"] >= duree_mini]
return chansons
print("---------", "\ncherche un titre...")
trouver = recherche(artiste, titre="shiver")
print("Trouvé:", trouver)
print("---------", "\ncherche une duree maxi...")
trouver = recherche(artiste, duree_max=2.7)
print("Trouvé:")
print(*trouver, sep="\n")
print("---------", "\ncherche une annee et une duree maxi...")
trouver = recherche(artiste, annee=2018, duree_max=2.9)
print("Trouvé:")
print(*trouver, sep="\n")
print("---------", "\ncherche un type et une duree mini...")
trouver = recherche(artiste, style="rock", duree_mini=4)
print("Trouvé:")
print(*trouver, sep="\n") |
Partager