Python 中的百分比和四舍五入
我有一个代码,它创建文本文件中某些单词长度的丰度百分比列表,例如 1 个字母的单词出现 13% 的时间,我想知道在 50,000 个单词的文本文件中是否有 1 个 20 个字母的单词,会它将二十个字母单词的百分比向下舍入到 0 或向上舍入到 1?
这是完整的代码:
lines = open ('E:\Videos, TV etc\Python\Assessment\dracula.txt', 'r'). readlines ()
stripped_list = [item.strip() for item in lines]
tally = [0] * 20
print tally #original tally
for i in stripped_list:
length_word = int(len(i))
tally[length_word-1] += 1 #adds 1 to the tally for the index of that word length, (length_word)-1 used as the tally for 1 letter words are in the 0 index
print tally
new_tally = [] #this tally will contain the occurences of each word length by percentage
for a in tally:
new_tally.append((100*a)/(sum(tally))) # multiplies by 100 and divides by all of the tallies to give a percentage
print new_tally
I have a code which creates a list of percentage abundance of certain word lengths in a text file e.g. 1 letter words appear 13% of the time, what I was wondering is if there was 1 twenty letter word in a 50,000 word text file, would it round the percentage for twenty letter words down to 0 or up to 1?
here is the entire code:
lines = open ('E:\Videos, TV etc\Python\Assessment\dracula.txt', 'r'). readlines ()
stripped_list = [item.strip() for item in lines]
tally = [0] * 20
print tally #original tally
for i in stripped_list:
length_word = int(len(i))
tally[length_word-1] += 1 #adds 1 to the tally for the index of that word length, (length_word)-1 used as the tally for 1 letter words are in the 0 index
print tally
new_tally = [] #this tally will contain the occurences of each word length by percentage
for a in tally:
new_tally.append((100*a)/(sum(tally))) # multiplies by 100 and divides by all of the tallies to give a percentage
print new_tally
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的代码使用整数楼层除法,它总是向零舍入。
通过使用浮点除法和 Python 的 round() 内置函数获得更多控制:
Your code is using integer floor division which always rounds towards zero.
Get more control by using floating point division and Python's round() builtin function:
默认情况下,如果分子和分母都是整数,您会得到截断的数字。
要解决实际百分比问题,您可以将一个或两个值更改为浮点数
,如果您谈论的是变量,
则乘以 100 即可得出百分比。
By default you got truncated number, if both numerator and denominator are integer.
to work around and real percentage, you change one or both of value to be floating poing number
And in case you are talking about variables,
multiply 100 to yield percentage.
假设您使用 int(),那么 Python总是向下舍入。 int(0.99999) = 0。实际上只是删除小数点后的部分。
如果您想要更像大多数人所说的四舍五入的意思,您可以这样做:
“%0.0f”%(yourval,)。
它使用了一个算法,它的名字我记不清了,其中数字正好在中间向最近的偶数舍入,所以 0.5 变成 0,但 1.5 变成 2。0.49 总是 0,0.51 是总是 1。
Assuming you are using int(), then Python always rounds down. int(0.99999) = 0. It's literally just dropping the part after decimal.
If you want something more like what most people mean by rounding, you can do:
"%0.0f" % (yourval,).
That uses an algorithmn, the name of which escapes me, where numbers exactly in the middle round towards the nearest even number, so 0.5 becomes 0, but 1.5 becomes 2. 0.49 is always 0, and 0.51 is always 1.
它将答案向下舍入为 0。
It would round the answer down to 0.