- 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))
10 Réponses
+ 3
First page found in google:
https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_list_count.asp
0
What is your aim here? Have you tried to use count function?
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)
0
Anyways... still we don't know precisely what's his aim
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."
0
so use my first solution ^^
0
Thanks
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)}%')
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
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)