Dictionnaire-{}
Les dictionnaires sont utilisés pour stocker les valeurs des données dans des paires clé:valeur.
Un dictionnaire est une collection ordonnée, modifiable et qui n'autorise pas les doublons.
Dans le dictionnaire, chaque élément est accessible par ses clés, et non par indexation.
Si le dictionnaire ne contient pas la clé, le résultat sera « KeyError ».
Exemple :
thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } student = {"name":"raja", "class":5} print(thisdict) print(student)
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964} {'name': 'raja', 'class': 5}
1. Trouver la fréquence de chaque lettre dans une chaîne
s = 'lakshmipritha' name = list(s) j = 0 while j<len(name): key = name[j] count = 1 i = j+1 if key != '*': while i<len(name): if key == name[i]: name[i] = '*' count+=1 i+=1 print(key, count) j+=1
l 1 a 2 k 1 s 1 h 2 m 1 i 2 p 1 r 1 t 1
2. Les lettres sont apparues une seule fois
s = 'lakshmipritha' name = list(s) j = 0 while j<len(name): key = name[j] count = 1 i = j+1 if key != '*': while i<len(name): if key == name[i]: name[i] = '*' count+=1 i+=1 if count == 1 and key!='*': print(key, count) j+=1
l 1 k 1 s 1 m 1 p 1 r 1 t 1
3. Lettre la plus fréquente
s = 'lakshmipritha' name = list(s) j = 0 while j<len(name): key = name[j] count = 1 i = j+1 if key != '*': while i<len(name): if key == name[i]: name[i] = '*' count+=1 i+=1 if count != 1 and key!='*': print(key, count) j+=1
a 2 h 2 i 2
4. Première lettre non répétée
s = 'lakshmipritha' name = list(s) j = 0 while j<len(name): key = name[j] count = 1 i = j+1 if key != '*': while i<len(name): if key == name[i]: name[i] = '*' count+=1 i+=1 if count == 1 and key!='*': print(key, count) break j+=1
l 1
5. Première lettre répétée
s = 'lakshmipritha' name = list(s) j = 0 while j<len(name): key = name[j] count = 1 i = j+1 if key != '*': while i<len(name): if key == name[i]: name[i] = '*' count+=1 i+=1 if count != 1 and key!='*': print(key, count) break j+=1
a 2
6. Dernière lettre non répétée
last = ' ' last_count = 0 s = 'lakshmipritha' name = list(s) j = 0 while j<len(name): key = name[j] count = 1 i = j+1 if key != '*': while i<len(name): if key == name[i]: name[i] = '*' count+=1 i+=1 if count == 1 and key!='*': last = key last_count = count #print(key, count) j+=1 print(last, last_count)
t 1
7. Dernière lettre répétée
last = ' ' last_count = 0 s = 'lakshmipritha' name = list(s) j = 0 while j<len(name): key = name[j] count = 1 i = j+1 if key != '*': while i<len(name): if key == name[i]: name[i] = '*' count+=1 i+=1 if count != 1 and key!='*': last = key last_count = count #print(key, count) j+=1 print(last, last_count)
i 2
8. Lettre la plus fréquente
s = 'lakshmipritha' name = list(s) j = 0 last = ' ' last_count = 0 while j<len(name): key = name[j] count = 1 i = j+1 if key != '*': while i<len(name): if key == name[i]: name[i] = '*' count+=1 i+=1 if count != 1 and key!='*': if count>last_count: last = key last_count = count j+=1 print(last, last_count)
a 2
9. Fréquence des voyelles (a,e,i,o,u)
vowels = ['a','e','i','o','u'] last = ' ' last_count = 0 s = 'lakshmipritha' name = list(s) j = 0 while j<len(name): key = name[j] if key in vowels: count = 1 i = j+1 if key != '*': while i<len(name): if key == name[i]: name[i] = '*' count+=1 i+=1 if key!='*': print(key, count) j+=1
a 2 i 2
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!