字典-{}
字典用于以键:值对的形式存储数据值。
字典是一个有序的、可更改的、不允许重复的集合。
在字典中,每个元素都可以通过它们的键来访问,而不是通过索引。
如果字典不包含该键,则输出将为“KeyError”。
示例:
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。查找字符串中每个字母的频率
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。字母只出现一次
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。最常见的字母
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。第一个不重复的字母
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。第一个重复的字母
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。最后一个不重复的字母
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。最后重复的字母
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。最常见的字母
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。元音出现频率 (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
以上是Day-Dictionary,使用嵌套循环的字符频率的详细内容。更多信息请关注PHP中文网其他相关文章!