Python,概率

发布于 2024-10-07 02:30:16 字数 1099 浏览 8 评论 0原文

接下来是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-小熊_ 2024-10-14 02:30:16

如果 frequencies = {"a": 3, "b": 4}frequencies.values() 给出 [3, 4]我们可以计算总和:

total = float(sum(frequencies.values()))

然后计算概率:

probs = [(count / total, char) for char, count in frequencies.iteritems()]

请注意,Python 在除两个整数时返回一个整数,这就是我首先将总和转换为浮点数的原因:

Python 2.7 (r27:82508, Jul  3 2010, 21:12:11) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 3 / 4
0
>>> 3 / 4.0
0.75

If frequencies = {"a": 3, "b": 4} then frequencies.values() gives us [3, 4] and we can calculate the sum:

total = float(sum(frequencies.values()))

and then the probabilities:

probs = [(count / total, char) for char, count in frequencies.iteritems()]

Note that Python returns an integer when dividing two integers, which was the reason I converted the sum into a float first:

Python 2.7 (r27:82508, Jul  3 2010, 21:12:11) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 3 / 4
0
>>> 3 / 4.0
0.75
超可爱的懒熊 2024-10-14 02:30:16

你快到了。

with open("test.txt") as f_in:
    for line in f_in:
        for char in line:
            frequencies[char] += 1
total = float(sum(frequencies.values()))
symbols = [(count/total, char) for char, count in frequencies.iteritems()]

请注意,我已重命名您的结果列表,因为 list 是内置函数的名称,您不应该使用它来命名变量或函数。

You're almost there.

with open("test.txt") as f_in:
    for line in f_in:
        for char in line:
            frequencies[char] += 1
total = float(sum(frequencies.values()))
symbols = [(count/total, char) for char, count in frequencies.iteritems()]

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.

两仪 2024-10-14 02:30:16

又快又脏:

   counter = 0
   with open("test.txt") as f_in:
        for line in f_in:
            for char in line:
                frequencies[char] += 1
                counter += 1

    list= [(count / counter, char) for char, count in frequencies.iteritems()]

Quick and dirty:

   counter = 0
   with open("test.txt") as f_in:
        for line in f_in:
            for char in line:
                frequencies[char] += 1
                counter += 1

    list= [(count / counter, char) for char, count in frequencies.iteritems()]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文