简体   繁体   中英

Logical error in a python program

alphabets ="abcdefghijklmnopqrstuvwxyz"
def calculation(text,alphabets):
    sample_list = []
    total_counter = 0
    for element in text:
        if element != " ":
            total_counter += 1
    total = total_counter

    for alphabet in alphabets:
        alphabet_counter = 0
        for element in text:
            if element == alphabet:
                alphabet_counter += 1
        tna = alphabet_counter
        percentage_counter = float((tna/total)*100)
        sample_list.append(percentage_counter)
    return sample_list

text = "sahib will be a very successful programmer one day."

x = calculation(text,alphabets)
print x

I was trying to make a python programmer which calculates the percentage of each character in a text. But When i print the list it displays an empty list

I think the line:

percentage_counter = float((tna/total)*100)

is doing integer math thus resulting in a so-called "floor division" that messes with the result. Try replacing with:

percentage_counter = 100*tna/float(total)

This is not an answer to your question (as user2464424 has already answered it), but just some tips for the future. Your function could be expressed quite more succinctly thus:

def calculation(text, alphabet):
    total = float(sum(ch in alphabet for ch in text))
    return [100 * sum(ch == letter for ch in text) / total for letter in alphabet]

This demonstrates Python's list comprehension , generator expressions , and the fact that booleans are integers .

It also fixes a bug in your program in that the total character count only excludes spaces, but not the period, while this version excludes all characters not explicitly in the given alphabet. (The list your current function returns does not sum to 100.)

You could simplify your problem immensely using Counter() , OrderedDict() and regex. Regex removes all characters that aren't AZ, Counter gets the number of occurrences for each letter and the OrderedDict lets you print the letters in order.

from collections import Counter, OrderedDict
import re

string= 'sahib will be a very successful programmer one day.'.lower()

string = re.sub('[^a-z]', '', string)

count = Counter(string)
count = OrderedDict(sorted(count.items()))

for k,v in count.items():
    print "{} {:.2f}".format(k, (v/float(len(string))*100))


a 9.52
b 4.76
c 4.76
d 2.38
e 11.90
f 2.38
g 2.38
h 2.38
i 4.76
l 7.14
m 4.76
n 2.38
o 4.76
p 2.38
r 9.52
s 9.52
u 4.76
v 2.38
w 2.38
y 4.76

I changed the end part to:

x = calculation(text,alphabets)
for val in x:
    print("%5.2f"%val)

and got a list that starts:

 9.30
 4.65
 4.65
 2.33
11.63
 2.33
 2.33

May I suggest that it would be more interesting if you stored the letter with the percentage? Try my version:

alphabets ="abcdefghijklmnopqrstuvwxyz"
def calculation(text,alphabets):
    sample_dict = {}
    total_counter = 0
    for element in text:
        if element != " ":
            total_counter += 1
    total = total_counter

    for alphabet in alphabets:
        alphabet_counter = 0
        for element in text:
            if element == alphabet:
                alphabet_counter += 1
        tna = alphabet_counter
        percentage_counter = float((tna/total)*100)
        sample_dict[alphabet]=(percentage_counter)
    return sample_dict

text = "the quack frown sox lumps oven the hazy fog"

x = calculation(text,alphabets)
for key,val in sorted(x.items()):
    print("%s"%key,"%5.2f"%val)

This results in a list that starts:

a  5.71
b  0.00
c  2.86
d  0.00
e  8.57
f  5.71
g  2.86
h  8.57
i  0.00

See what you think.

Ohk ,in Simple terms try printing float(tna/total) . You will get 0 since the value inside outputs 0 (only integer) and float of 0 is nothing but 0.0.

So here is what you can do- Convert one of tna or total to a float before performing tna/total. like tna*1.0000/total. This way it works. Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM