Python,概率
接下来是我的代码:
with open("test.txt") as f_in:
for line in f_in:
for char in line:
frequencies[char] += 1
list= [(count, char) for char, count in frequencies.iteritems()]
此代码打开 test.txt,读取每一行并将“列表”符号放入表单中,例如:[(3, 'a'),...... ...]。这意味着在整个文本文件中,有三个 a 等等...
我需要计算这个数字,而不是 3,我需要 [ 3 / 所有符号的数量]。所以我不需要文本中有多少个符号,例如 a,但我需要符号 a 的概率。
因此,如果在文本(test.txt)中会有 "aaab",我需要 "list" 的输出: [(0.75, 'a'), (0.25, 'b') ]
非常感谢您的帮助。
EDIT2
import collections
frequencies = collections.defaultdict(int)
with open("test.txt") as f_in:
for line in f_in:
for char in line:
frequencies[char] += 1
total = float(sum(frequencies.keys()))
verj= [(count/total, char) for char, count in frequencies.iteritems()]
这不起作用,给我错误:
总计 = float(sum(frequencies.keys()))
类型错误:+ 不支持的操作数类型:“int”和“str”
My code is next:
with open("test.txt") as f_in:
for line in f_in:
for char in line:
frequencies[char] += 1
list= [(count, char) for char, count in frequencies.iteritems()]
This code open test.txt, read every line and "list" sign into form for example: [(3, 'a'),.........]. This means that in whole text file, there are three a and so on...
What I need is to calculate for this number, instead 3, I need [ 3 / number of all sign ]. So I don't need number of how many sign for example a is in text, but I need probability of sign a.
So if in text(test.txt) there will be "aaab", I need output of "list": [(0.75, 'a'), (0.25, 'b')]
Many thanks for help.
EDIT2
import collections
frequencies = collections.defaultdict(int)
with open("test.txt") as f_in:
for line in f_in:
for char in line:
frequencies[char] += 1
total = float(sum(frequencies.keys()))
verj= [(count/total, char) for char, count in frequencies.iteritems()]
This not working, give me error:
total = float(sum(frequencies.keys()))
TypeError: unsupported operand type(s) for +: 'int' and 'str'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果
frequencies = {"a": 3, "b": 4}
则frequencies.values()
给出[3, 4]
我们可以计算总和:然后计算概率:
请注意,Python 在除两个整数时返回一个整数,这就是我首先将总和转换为浮点数的原因:
If
frequencies = {"a": 3, "b": 4}
thenfrequencies.values()
gives us[3, 4]
and we can calculate the sum:and then the probabilities:
Note that Python returns an integer when dividing two integers, which was the reason I converted the sum into a float first:
你快到了。
请注意,我已重命名您的结果列表,因为
list
是内置函数的名称,您不应该使用它来命名变量或函数。You're almost there.
Note that I've renamed your resulting list because
list
is the name of a built-in and you shouldn't use it to name variables or functions.又快又脏:
Quick and dirty: