What am I doing wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

What am I doing wrong?

Letter frequency project in python. x = [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z] for l in x: print(len(l*100))

24th Feb 2021, 1:04 PM
Michael Gonzalez
Michael Gonzalez - avatar
10 Answers
24th Feb 2021, 1:16 PM
Tomas Konecny
0
What is your aim here? Have you tried to use count function?
24th Feb 2021, 1:16 PM
Tomas Konecny
0
the count() solution is acceptable for tiny inputs or only one characte frequency needed, but if you need to deal with big inputs (either a lot of inputs, or big inputed strings) and/or with all characters frequencies needed, you rather would iterate (only once by string) over the input string and count 'manually' occurence by filling/updating a dict whose keys are encountered characters, and values is the count of that character only one character (or any string length) frequency in string or list: print(string_or_list.count(char)) all characters frequency: freq = {} for char in string_or_list: if char in freq: freq[char] += 1 else: freq[char] = 1 print(freq)
24th Feb 2021, 4:14 PM
visph
visph - avatar
0
Anyways... still we don't know precisely what's his aim
24th Feb 2021, 4:16 PM
Tomas Konecny
0
If you were to read my description it would clearly tell you what my aim is. Considering you use this, but here is my aim, "You are making a program to analyze text. Take the text as the first input and a letter as the second input, and output the frequency of that letter in the text as a whole percentage. Sample Input: hello l Sample Output: 40 The letter l appears 2 times in the text hello, which has 5 letters. So, the frequency would be (2/5)*100 = 40."
24th Feb 2021, 4:31 PM
Michael Gonzalez
Michael Gonzalez - avatar
0
so use my first solution ^^
24th Feb 2021, 4:32 PM
visph
visph - avatar
0
Thanks
24th Feb 2021, 4:33 PM
Michael Gonzalez
Michael Gonzalez - avatar
0
You can try to use this, if you only need the frequency of lowercase letters: import string s = "hello" x = string.ascii_lowercase for l in x: if l in s: print(f'{l}: {int((s.count(l)/len(s))*100)}%')
24th Feb 2021, 4:49 PM
Tomas Konecny
0
Michael Gonzalez #your code goes here string = input("") string1 = input("") number = 0 i = string1 for i in string: if i == string1: number += 1 #print(number) len = len(string) calc = number/len*100 print(int(calc)) #This code worked perfectly for me, Hope this helps You
12th Mar 2021, 6:15 AM
Mohamed Alaan
Mohamed Alaan - avatar
0
text = input("") letter = input("") textList = [x for x in text if x != ' '] letterCount = text.count(letter) result = int(letterCount/len(text)*100) print(result)
22nd Apr 2021, 8:40 PM
Ana Tulea
Ana Tulea - avatar