首页 > 后端开发 > Python教程 > Day-Dictionary,使用嵌套循环的字符频率

Day-Dictionary,使用嵌套循环的字符频率

Susan Sarandon
发布: 2025-01-02 19:27:10
原创
1025 人浏览过

Day-Dictionary, Frequency of character using nested loops

字典-{}

字典用于以键:值对的形式存储数据值。
字典是一个有序的、可更改的、不允许重复的集合。
在字典中,每个元素都可以通过它们的键来访问,而不是通过索引。
如果字典不包含该键,则输出将为“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中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板