Home > Backend Development > Python Tutorial > Day-Dictionary, Frequency of character using nested loops

Day-Dictionary, Frequency of character using nested loops

Susan Sarandon
Release: 2025-01-02 19:27:10
Original
1034 people have browsed it

Day-Dictionary, Frequency of character using nested loops

Dictionary-{}

Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is ordered, changeable and do not allow duplicates.
In dictionary each element can be accessed by their keys, not through indexing.
If dictionary does not contain the key then the output will be 'KeyError'.

Example:

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

student = {"name":"raja", "class":5}

print(thisdict)
print(student)
Copy after login
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
{'name': 'raja', 'class': 5}

Copy after login

1. Finding frequency of each letter in a string

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

Copy after login
l 1
a 2
k 1
s 1
h 2
m 1
i 2
p 1
r 1
t 1

Copy after login

2. Letters appeared Only Once

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

Copy after login
l 1
k 1
s 1
m 1
p 1
r 1
t 1

Copy after login

3. Most frequent letter

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
Copy after login
a 2
h 2
i 2
Copy after login

4. First Non-repeated letter

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
Copy after login
l 1
Copy after login

5. First repeated letter

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
Copy after login
a 2
Copy after login
Copy after login

6. Last non-repeated letter

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)
Copy after login
t 1
Copy after login

7. Last repeated letter

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)
Copy after login
i 2
Copy after login

8. Most Frequent letter

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)
Copy after login
a 2
Copy after login
Copy after login

9. Frequency of Vowels (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

Copy after login
a 2
i 2
Copy after login

The above is the detailed content of Day-Dictionary, Frequency of character using nested loops. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template