首页 \ 问答 \ Python - 计算文本文件中的单词(Python - Counting Words In A Text File)

Python - 计算文本文件中的单词(Python - Counting Words In A Text File)

我是Python的新手,正在开发一个程序,它将计算简单文本文件中的单词实例。 程序和文本文件将从命令行中读取,因此我已将其包含在我的编程语法中以检查命令行参数。 代码如下

import sys

count={}

with open(sys.argv[1],'r') as f:
    for line in f:
        for word in line.split():
            if word not in count:
                count[word] = 1
            else:
                count[word] += 1

print(word,count[word])

file.close()

count是一个字典,用于存储单词及其出现次数。 我希望能够打印出每个单词及其出现的次数,从大多数事件开始到最少出现。

我想知道我是否在正确的轨道上,如果我正在使用系统。 谢谢!!


I'm new to Python and am working on a program that will count the instances of words in a simple text file. The program and the text file will be read from the command line, so I have included into my programming syntax for checking command line arguments. The code is below

import sys

count={}

with open(sys.argv[1],'r') as f:
    for line in f:
        for word in line.split():
            if word not in count:
                count[word] = 1
            else:
                count[word] += 1

print(word,count[word])

file.close()

count is a dictionary to store the words and the number of times they occur. I want to be able to print out each word and the number of times it occurs, starting from most occurrences to least occurrences.

I'd like to know if I'm on the right track, and if I'm using sys properly. Thank you!!


原文:https://stackoverflow.com/questions/25778341
更新时间:2022-02-03 11:02

最满意答案

我会使用Angular的pub / sub模式

这是一篇关于最佳实践的文章

我会有一堆与自己的范围数据交互的指令,当指令中的数据发生更改时,它可以$ broadcast('message',data)对该数据的更改,其他指令可以监听$ on('message',function(数据){})

如果您希望一切都依赖于相同的数据,那么只需使用所有指令与之交互的服务(单例)


i would use Angular's pub/sub pattern

Here is an article the talks about best practices

I would have a bunch of directives that interact with there own scope data, when data changes in a directive it can $broadcast('message', data) a change to that data other directives can listen $on('message', function(data){})

If you want everything to rely on the same data, then just use a service (singleton) that all the directive interact with

相关问答

更多
  • 你需要移动div
  • attrs的目的是什么? 与您的指令相同的元素定义的属性有几个目的: 它们是将信息传递到使用隔离范围的指令的唯一方法。 由于伪指令隔离范围并不从父范围继承,我们需要一种方法来指定我们要传递给隔离范围的内容。 因此,“对象散列”中的'@','='和'&'需要一个属性来指定要传递的数据/信息。 它们作为互通通信机制。 (例如, 独立管理独立AngularJS指令之间的沟通 ) 它们归一化属性名称。 无法访问通过attrs传递的所有属性? 是的,可以,但是 您将不会有任何数据绑定。 '@'设置单向“字符串”数据绑 ...
  • 由于该指令只调用一个函数(而不是尝试在属性上设置一个值),所以可以使用$ eval而不是$ parse(具有非隔离范围): scope.$apply(function() { scope.$eval(attrs.confirmAction); }); 或者更好的是,只需使用$ apply ,这将使$ eval()使其参数与范围相对应: scope.$apply(attrs.confirmAction); 小提琴 Since the directive is only calling a func ...
  • 请参阅角度元素API文档 。 如果使用element.scope(),您将获得在指令的scope属性中定义的元素的范围。 如果使用element.isolateScope(),您将获得整个隔离的作用域。 例如,如果您的指令看起来像这样: scope : { myScopeThingy : '=' }, controller : function($scope){ $scope.myIsolatedThingy = 'some value'; } 那么在你的测试中调用element.scope()会返回 ...
  • 您发布的代码没有问题,除了&iframeCallBack应该是&iframeCallback 。 否则,请考虑将data-iframe-callback="testLoad()"更改为data-iframe-call-back="testLoad()" 。 问题的可能原因是您有多个指令是通过复制粘贴指令的现有代码而创建的,并且忘记将指令的名称更改为新指令。 只需在代码中搜索iframeOnLoad ,我很确定你会发现两个带有这种名称的指令。 它正在发生,因为AngularJS允许注册具有相同名称的多个指令。 ...
  • 对于值,您应该在范围对象中使用“=”而不是“@”。 此外,为您的范围定义“name2”而不是val。 或者将val链接到name2属性,我不明白为什么你在val上使用$ watch,数据绑定就是这样做的,所以这样做 app.directive("helloWorld", function() { return { restrict: "E", scope: { name: "=", val: "=name2" }, template: "
    a {{nam ...
  • 对此有一个简单的答案,适用于此处的所有示例。 关于$ compile的Angular文档解释了这一点,但很容易误解。 隔离范围的整个目的是创建一个仅由声明它的指令消耗的范围。 为此,将创建一个新变量,该值将值存储为父作用域的别名。 有三种主要的定义类型: @ , = , & @或@attr - 将本地范围属性绑定到DOM属性的值。 结果总是一个字符串,因为DOM属性是字符串。 = or = attr - 在本地范围属性和通过attr属性的值定义的name的父范围属性之间设置双向绑定。 &or&attr - ...
  • 我会使用Angular的pub / sub模式 这是一篇关于最佳实践的文章 我会有一堆与自己的范围数据交互的指令,当指令中的数据发生更改时,它可以$ broadcast('message',data)对该数据的更改,其他指令可以监听$ on('message',function(数据){}) 如果您希望一切都依赖于相同的数据,那么只需使用所有指令与之交互的服务(单例) i would use Angular's pub/sub pattern Here is an article the talks abo ...
  • 原因是,您必须在指令的template属性中输入template ,使其成为孤立的template 。 现在,该指令创建了一个独立的范围,但它不会在任何地方使用它,因为当指令的链接函数被触发时,指令标记内的内容已经在父范围(MyCtrl)中进行了评估。 这可能是想要做的事http://plnkr.co/edit/jmWrNpLFttDPhSooPF0M?p=preview 指示 .directive('myIsolatedScopedDirective', function(){ return { ...
  • 使用隔离范围时,控制器范围的setSelectedItem方法是不可见的。 解: 将setSelectedItem添加到指令isoalted作用域。 +添加双向数据绑定以进行预测。 工作地点: http : //plnkr.co/edit/e5ApIg?p = preview 所做的更改是: app.directive('myWeather', function() { return { restrict: 'EA', transclude: 'true', //If I com ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。