python 程序中的逻辑错误
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
我试图制作一个 python 程序员来计算文本中每个字符的百分比。但是当我打印列表时,它显示一个空列表
我认为行:
percentage_counter = float((tna/total)*100)
正在做整数数学运算,从而导致所谓的 "floor division" 弄乱了结果。
尝试替换为:
percentage_counter = 100*tna/float(total)
您可以使用 Counter()
、OrderedDict()
和正则表达式极大地简化您的问题。 Regex 删除所有不是 A-Z 的字符,Counter 获取每个字母的出现次数,OrderedDict 允许您按顺序打印字母。
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
这不是您问题的答案(因为 user2464424 已经回答了),而只是对未来的一些提示。您的功能可以更简洁地表达为:
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]
这演示了 Python 的 list comprehension, generator expressions, and the fact that booleans are integers。
它还修复了程序中的一个错误,即总字符数仅排除空格,但不排除句点,而此版本排除了所有未明确出现在给定字母表中的字符。 (您当前函数的列表 returns 总和不等于 100。)
我把结尾部分改成了:
x = calculation(text,alphabets)
for val in x:
print("%5.2f"%val)
并得到一个开始于:
的列表
9.30
4.65
4.65
2.33
11.63
2.33
2.33
我建议如果你把字母和百分比一起存储会更有趣吗?试试我的版本:
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)
这导致列表开始:
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
看看你的想法。
Ohk,简单来说,尝试打印 float(tna/total) 。您将得到 0,因为输出 0 中的值(仅整数)和 0 的浮点数不过是 0.0.
因此,您可以执行以下操作 - 在执行 tna/total 之前将 tna 或 total 之一转换为浮点数。
比如 tna*1.0000/总计。
这样就可以了。
干杯
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
我试图制作一个 python 程序员来计算文本中每个字符的百分比。但是当我打印列表时,它显示一个空列表
我认为行:
percentage_counter = float((tna/total)*100)
正在做整数数学运算,从而导致所谓的 "floor division" 弄乱了结果。 尝试替换为:
percentage_counter = 100*tna/float(total)
您可以使用 Counter()
、OrderedDict()
和正则表达式极大地简化您的问题。 Regex 删除所有不是 A-Z 的字符,Counter 获取每个字母的出现次数,OrderedDict 允许您按顺序打印字母。
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
这不是您问题的答案(因为 user2464424 已经回答了),而只是对未来的一些提示。您的功能可以更简洁地表达为:
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]
这演示了 Python 的 list comprehension, generator expressions, and the fact that booleans are integers。
它还修复了程序中的一个错误,即总字符数仅排除空格,但不排除句点,而此版本排除了所有未明确出现在给定字母表中的字符。 (您当前函数的列表 returns 总和不等于 100。)
我把结尾部分改成了:
x = calculation(text,alphabets)
for val in x:
print("%5.2f"%val)
并得到一个开始于:
的列表 9.30
4.65
4.65
2.33
11.63
2.33
2.33
我建议如果你把字母和百分比一起存储会更有趣吗?试试我的版本:
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)
这导致列表开始:
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
看看你的想法。
Ohk,简单来说,尝试打印 float(tna/total) 。您将得到 0,因为输出 0 中的值(仅整数)和 0 的浮点数不过是 0.0.
因此,您可以执行以下操作 - 在执行 tna/total 之前将 tna 或 total 之一转换为浮点数。 比如 tna*1.0000/总计。 这样就可以了。 干杯