python程序中的逻辑错误

python程序中的逻辑错误,python,python-2.x,Python,Python 2.x,我试图制作一个python程序员,它计算文本中每个字符的百分比。但是当我打印列表时,它会显示一个空列表我认为行: alphabets ="abcdefghijklmnopqrstuvwxyz" def calculation(text,alphabets): sample_list = [] total_counter = 0 for element in text: if element != " ": total_counter

我试图制作一个python程序员,它计算文本中每个字符的百分比。但是当我打印列表时,它会显示一个空列表

我认为行:

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
正在进行整数运算,从而导致所谓的“地板分割”,从而扰乱结果。 尝试替换为:

percentage_counter = float((tna/total)*100)
我认为这句话:

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
正在进行整数运算,从而导致所谓的“地板分割”,从而扰乱结果。 尝试替换为:

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

您可以使用
Counter()
orderedict()和regex极大地简化问题。正则表达式删除所有非A-Z字符,计数器获取每个字母的出现次数,OrderedDict允许您按顺序打印字母

percentage_counter = 100*tna/float(total)

您可以使用
Counter()
orderedict()和regex极大地简化问题。正则表达式删除所有非A-Z字符,计数器获取每个字母的出现次数,OrderedDict允许您按顺序打印字母

percentage_counter = 100*tna/float(total)

这不是对您的问题的回答(正如用户2464424已经回答的那样),而是对未来的一些提示。您的函数可以更简洁地表达,因此:

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
这展示了Python的特性,以及布尔值的事实


它还修复了程序中的一个错误,即字符总数仅排除空格,而不排除句点,而此版本排除了给定字母表中未明确显示的所有字符。(当前函数返回的列表总和不等于100。)

这不是对您的问题的回答(因为用户2464424已经回答了这个问题),而是对未来的一些提示。您的函数可以更简洁地表达,因此:

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
这展示了Python的特性,以及布尔值的事实


它还修复了程序中的一个错误,即字符总数仅排除空格,而不排除句点,而此版本排除了给定字母表中未明确显示的所有字符。(当前函数返回的列表总和不是100。)

我将结束部分更改为:

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]
并得到了一个开始的列表:

x = calculation(text,alphabets)
for val in x:
    print("%5.2f"%val)
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)
我可以建议,如果你把这封信和百分比一起存储,会更有趣吗?试试我的版本:

 9.30
 4.65
 4.65
 2.33
11.63
 2.33
 2.33
这将产生一个列表,该列表将开始:

x = calculation(text,alphabets)
for val in x:
    print("%5.2f"%val)
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)

看看你怎么想。

我把结尾部分改为:

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]
并得到了一个开始的列表:

x = calculation(text,alphabets)
for val in x:
    print("%5.2f"%val)
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)
我可以建议,如果你把这封信和百分比一起存储,会更有趣吗?试试我的版本:

 9.30
 4.65
 4.65
 2.33
11.63
 2.33
 2.33
这将产生一个列表,该列表将开始:

x = calculation(text,alphabets)
for val in x:
    print("%5.2f"%val)
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)

看看你的想法。

哦,简单地说,试着打印float(tna/total)。您将得到0,因为内部的值输出0(仅整数),0的浮点值仅为0.0

下面是您可以做的-在执行tna/total之前,将tna或total中的一个转换为浮点。 比如tna*1.0000/总计。 这样就行了。
干杯

哦,简单地说,试着打印float(tna/total)。您将得到0,因为内部的值输出0(仅整数),0的浮点值仅为0.0

下面是您可以做的-在执行tna/total之前,将tna或total中的一个转换为浮点。 比如tna*1.0000/总计。 这样就行了。
干杯

你说的“空名单”是指由零组成的名单,还是实际上是空的名单?“空列表”通常意味着后者,但当我运行程序时,它显示的是前者。将
percentage\u counter=float((tna/total)*100)
更改为
percentage\u counter=(float(tna)/float(total))*100
此外,您应该将示例列表更改为
sample\u list.append((字母表,percentage\u counter))
,通过这种方式,您可以将百分比分配给相应的字符/并且您可以
取整
百分比。通过“空列表”,您是指零列表还是实际为空的列表?“空列表”通常意味着后者,但当我运行程序时,它显示的是前者。将
percentage\u counter=float((tna/total)*100)
更改为
percentage\u counter=(float(tna)/float(total))*100
此外,您应该将示例列表更改为
sample\u list.append((字母表,percentage\u counter))
,通过这种方式,您可以将百分比分配给相应的字符/并且您可以
舍入
百分比“因此舍入结果”--应该注意的是,结果在整数除法中不是“舍入”的,而是“四舍五入”的在整数除法中,但在d层,它比X更有效,兄弟