[Tutor] I need help

Alan Gauld alan.gauld at yahoo.co.uk
Tue Jul 6 04:53:31 EDT 2021


On 06/07/2021 01:32, edna rey wrote:

> E O O E E O O O R R R V J Z Z Z B
> 
> E O E O R V J Z B
> 
> 1 2 2 3 3 1 1 3 1

Notice that you need two result lists not one.
The first stores the unique letters
The second stores the frequency counts.

Python provides a solution for that in the form
of a dictionary, but I'm going to assume that
your course has not covered them yet and work
with simple lists. (There is an even more
specific tool for this kind of scenario in
the Collections module, but I'll ignore that too!)

> letras= input(str ("IngreselasInicialesDelosActores")).upper()

You don't need the str() call since it is already a string.

> frecuencia = []

Here you create an empty list. If it was a dictionary
 - do you know about them yet? The code below would work.
But with lists it won't. You would need to size the list
to store all of the possible letters. You an do that like:

frecuencia = 'abcdefghijklmnopqrstuvwxyz'.upper()

You also need a third list to store the counts:

counts = [0] * len(frecuencia)

> for n in (len(letras)):

This gets the index of a letter, you probably just want
the letter

for c in letras:

>     if n in frecuencia:
>         frecuencia[n] +=1

This then looks to see if the index is in the second list.
You probably want to see where the letter is in the second list:

index = frecuencia.index(c)

Now you can update the counts list:

counts[index] += 1


>     else:
>         frecuencia[n] = 1

Because we initialized the counts to zero you don't
need the else clause.

> print(frecuencia)

You need to print only the letters used and their counts so:

for n in range(len(frecuencia)):
    if counts[n] > 0:
       print(frecuencia[n],':',counts[n])


> IngreselasInicialesDelosActoresG G U U U U A A A Z Z Z T T T V V Q Q A A A
> G G U U U U A A A Z Z Z T T T V V Q Q A A A
> 43
> {'G': 2, ' ': 21, 'U': 4, 'A': 6, 'Z': 3, 'T': 3, 'V': 2, 'Q': 2}

This last line suggests you are familiar with dictionaries?
If so there is a simpler solution using a dictionary.

Note that the solution above assumes minimal knowledge of Python.
It does not show the best way to do it in Python, just a very
basic technique that hopefully you can understand.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list