Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.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 2.6 - Fatal编程技术网

Python中的示例函数:计算单词数

Python中的示例函数:计算单词数,python,python-2.6,Python,Python 2.6,我对Python有点生疏,我只是在寻找帮助,以实现一个示例函数来计算单词数(这只是scons脚本的一个示例目标,它不做任何“真实”的事情): 你能帮我填一下细节吗?计算单词的通常方法是阅读每一行,分成单词,并为每一行中的每个单词在字典中增加一个计数器;然后,对于输出,通过减少计数对单词进行排序 编辑:我正在使用Python2.6(确切地说是Python2.6.5)有一个很有用的例子。它的工作原理与您描述的大致相同,并且可以计算句子。这里有一个有用的示例。它的工作原理与您所描述的大致相同,并且还可

我对Python有点生疏,我只是在寻找帮助,以实现一个示例函数来计算单词数(这只是scons脚本的一个示例目标,它不做任何“真实”的事情):

你能帮我填一下细节吗?计算单词的通常方法是阅读每一行,分成单词,并为每一行中的每个单词在字典中增加一个计数器;然后,对于输出,通过减少计数对单词进行排序


编辑:我正在使用Python2.6(确切地说是Python2.6.5)

有一个很有用的例子。它的工作原理与您描述的大致相同,并且可以计算句子。

这里有一个有用的示例。它的工作原理与您所描述的大致相同,并且还可以计算句子。

在不知道为什么
env
存在的情况下,我只能执行以下操作:

def countWords(target, source, env):
    wordCount = {}
    if len(target) == 1 and len(source) == 1:
        with fin as open(source[0], 'r'):
            for line in f
                for word in line.split():
                    if word in wordCount.keys():
                        wordCount[word] += 1
                    else:
                        wordCount[word] = 0

        rev = {}
        for v in wordCount.values():
            rev[v] = []
        for w in wordCount.keys():
            rev[wordCOunt[w]].append(w)
        with open(target[0], 'w') as f:
            for v in rev.keys():
                f.write("%d: %s\n" %(v, " ".join(rev[v])))

在不知道为什么
env
存在的情况下,我只能执行以下操作:

def countWords(target, source, env):
    wordCount = {}
    if len(target) == 1 and len(source) == 1:
        with fin as open(source[0], 'r'):
            for line in f
                for word in line.split():
                    if word in wordCount.keys():
                        wordCount[word] += 1
                    else:
                        wordCount[word] = 0

        rev = {}
        for v in wordCount.values():
            rev[v] = []
        for w in wordCount.keys():
            rev[wordCOunt[w]].append(w)
        with open(target[0], 'w') as f:
            for v in rev.keys():
                f.write("%d: %s\n" %(v, " ".join(rev[v])))

不是很有效率,但是很简洁

with open(fname) as f:
   res = {}
   for word in f.read().split():
       res[word] = res.get(word, 0)+1
with open(dest, 'w') as f:
    f.write("\n".join(sorted(res, key=lambda w: -res[w])))

不是很有效率,但是很简洁

with open(fname) as f:
   res = {}
   for word in f.read().split():
       res[word] = res.get(word, 0)+1
with open(dest, 'w') as f:
    f.write("\n".join(sorted(res, key=lambda w: -res[w])))
以下是我的版本:

import string
import itertools as it
drop = string.punctuation+string.digits

def countWords(target, source, env=''):
    inputstring=open(source).read()
    words = sorted(word.strip(drop)
                   for word in inputstring.lower().replace('--',' ').split())
    wordlist = sorted([(word, len(list(occurances)))
                      for word, occurances in it.groupby(words, lambda x: x)],
                        key = lambda x: x[1],
                      reverse = True)
    with open(target,'w') as results:
        results.write('\n'.join('%16s : %s' % word for word in wordlist))
以下是我的版本:

import string
import itertools as it
drop = string.punctuation+string.digits

def countWords(target, source, env=''):
    inputstring=open(source).read()
    words = sorted(word.strip(drop)
                   for word in inputstring.lower().replace('--',' ').split())
    wordlist = sorted([(word, len(list(occurances)))
                      for word, occurances in it.groupby(words, lambda x: x)],
                        key = lambda x: x[1],
                      reverse = True)
    with open(target,'w') as results:
        results.write('\n'.join('%16s : %s' % word for word in wordlist))

env
的预期用途是什么?忽略它。这是一个scons的东西,在这种情况下什么都不做。你使用的是Python2.7还是更好?有一个很好的
集合。如果是,可以使用Counter
对象…env的预期用途是什么?忽略它。这是一个scons的东西,在这种情况下什么都不做。你使用的是Python2.7还是更好?有一个很好的
集合。计数器
对象,如果可以的话,你可以使用它…谢谢,但我已经找到了那个,它不符合我的要求(我想记下每个单词的数量)谢谢,但我已经找到了那个,它不符合我的要求(我想记下每个单词的数量)顺便说一句,这里使用的
有什么作用?为什么不干脆
fin=open(str(source[0]),'r')
取而代之呢?@Jason-fixed
reverse
(这是个打字错误,谢谢!)<使用
可以在一个块内打开/关闭文件。哦,应该是自动关闭块吗?(就像C++中的RAII模式?)杰森:是的,我想这就是目标。顺便问一下,这里使用的
是什么?为什么不干脆
fin=open(str(source[0]),'r')
取而代之呢?@Jason-fixed
reverse
(这是个打字错误,谢谢!)<使用
可以在一个块内打开/关闭文件。哦,应该是自动关闭块吗?(就像C++中的RAII模式?)杰森:是的,我想这就是目标。检查