Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写测量每行(对象)频率的函数-Python_Python_Python 3.x - Fatal编程技术网

如何编写测量每行(对象)频率的函数-Python

如何编写测量每行(对象)频率的函数-Python,python,python-3.x,Python,Python 3.x,编写一个函数create_dictionary(filename),该函数读取命名文件并返回从对象名称到出现计数(猜测特定对象的次数)的字典映射。例如,给定包含以下内容的文件mydata.txt: abacus calculator modern computer abacus modern computer large white thing modern computer 因此,当我输入以下内容时: dictionary = create_dictionary('mydata.txt')

编写一个函数create_dictionary(filename),该函数读取命名文件并返回从对象名称到出现计数(猜测特定对象的次数)的字典映射。例如,给定包含以下内容的文件mydata.txt

abacus
calculator
modern computer
abacus
modern computer
large white thing
modern computer
因此,当我输入以下内容时:

dictionary = create_dictionary('mydata.txt')
for key in dictionary:
print(key + ': ' + str(dictionary[key]))
函数必须返回以下字典格式:

{'abacus': 2, 'calculator': 1, 'modern computer': 3, 'large white thing': 1}
除此之外,我知道如何计算单词的频率。但是如何计算上述每一行的频率呢

以下是一些限制条件:

  • 您可以假设给定的文件存在,但它可能是空的(即。 不包含行)
  • 键必须按其插入的顺序插入词典 显示在输入文件中
  • 在一些测试中,我们按插入顺序显示键;另一些则是按字母顺序排列钥匙
  • 应该从对象名称中去掉前导和尾随空格
  • 空对象名称(例如,空行或仅带空格的行) 应该被忽略

一个更容易实现的方法是使用以下方法

让文件名
a.txt

from collections import Counter
s = open('a.txt','r').read().strip()
print(Counter(s.split('\n')))
输出结果如下:

Counter({'abacus': 2,
         'calculator': 1,
         'large white thing': 1,
         'modern computer': 3})

除了@bigbounty的建议,这里是我能想到的

from collections import Counter
def create_dictionary(filename):
    """Blah"""
    keys = Counter()
    s = open(filename,'r').read().strip()
    keys = (Counter(s.split('\n')))
    return keys
因此,如果我键入:

dictionary = create_dictionary('mydata.txt')
for key in dictionary:
   print(key + ': ' + str(dictionary[key]))
我得到:

abacus: 2
calculator: 1
modern computer: 3
large white thing: 1
但是我需要一些关于“如果文本文件为空,如何不打印内容”的帮助


例如:考虑一个空文本文件(“没什么。txt”)。预期输出为空。但是我不知道如何省略键的默认值“:1”。有什么建议吗?

到目前为止你做了什么?就像我说的,现在,我正在咀嚼我的表皮。我知道如何处理文本文件中的单词,但是词组的频率,我瘫痪了。好的,在python中使用dict。请澄清如何在这篇文章中应用词典case@Pamela,那么如何计算文本文件中的字数呢?如果我们使用我提供的输出脚本,答案仍然有问题。
print()中缺少右括号
语句。即使编辑了paranthesis,它也不起作用……不是预期的格式。此外,“计数器”不存在(如预期输出示例所示。如果文件为空,则可以使用
open('a.txt','r')。read()==''
结果为true,但函数必须识别文本文件是否为空,如果为空,函数必须打印为空。我如何才能做到这一点?目前我得到的输出是“:1”。但我需要输出为空。