计算发电机中的物品 [英] count items in generator
问题描述
还是新的。我正在尝试制作一个简单的单词计数脚本。
我在伟大的Python Cookbook中找到了这个,它允许我处理
文件中的每个单词。但是如何使用它来计算生成的项目?
def words_of_file(thefilepath,line_to_words = str.split):
the_file = open(thefilepath)
$ _ $ b for the line in the_file:
for line in line_to_words(line):
yield word
the_file.close ()
单词words_of_file(thefilepath):
dosomethingwith(单词)
我能想到的最好的:
def words_of_file(thefilepath,line_to_words = str.split):
the_file = open(thefilepath)
for the file in the_file :
for line_to_words(line):
yield word
the_file.close()
len( list(words_of_file(thefilepath)))
但是这看起来很笨重。
BartlebyScrivener< rp ******* @ gmail.com>写道:
还是新的。我正在尝试制作一个简单的字数统计脚本。
我在伟大的Python Cookbook中找到了这个,它允许我处理文件中的每个单词。但是如何使用它来计算生成的项目?
def words_of_file(thefilepath,line_to_words = str.split):
the_file = open(thefilepath)
for the file in the_file :
for line_to_words(line):
产生字
the_file.close()
用于words_of_file(thefilepath)中的单词:
dosomethingwith(word)
我能想到的最好的:
def words_of_file(thefilepath,line_to_words = str.split):
the_file = open(thefilepath)
for line在the_file中:
for line_to_words(line):
yield word
the_file.close()
len(list(words_of_file(thefilepath)))
我的偏好(用原始定义为
words_of_the_file)代码
numwords = sum(word_of_the_file中的w为1(文件路径))
Alex
BartlebyScrivener写道:
还是新的。我正在尝试制作一个简单的字数统计脚本。
我在伟大的Python Cookbook中找到了这个,它允许我处理文件中的每个单词。但是如何使用它来计算生成的项目?
def words_of_file(thefilepath,line_to_words = str.split):
the_file = open(thefilepath)
for the file in the_file :
for line_to_words(line):
产生字
the_file.close()
用于words_of_file(thefilepath)中的单词:
dosomethingwith(word)
我能想到的最好的:
def words_of_file(thefilepath,line_to_words = str.split):
the_file = open(thefilepath)
for line在the_file中:
for line_to_words(line):
yield word
the_file.close()
len(list(words_of_file(thefilepath)))
看起来很笨重,我认为你无法击败它。
简洁;如果你关心内存效率,那么这就是我使用的:
def length(可迭代):
try:return len(iterable)
除外:
i = 0
for x in iterable:i + = 1
返回i
如果你愿意,你甚至可以隐藏内置len():
import __builtin__
def len(可迭代):
尝试:返回__builtin __。len(可迭代)
除外:
i = 0
for x在iterable中:i + = 1
返回i
HTH,
George
blockquote>
谢谢!并感谢Cookbook。
rd
没有抽象艺术。你必须始终从某事开始。
之后你可以删除所有现实痕迹。 - Pablo Picasso
Still new. I am trying to make a simple word count script.
I found this in the great Python Cookbook, which allows me to process
every word in a file. But how do I use it to count the items generated?
def words_of_file(thefilepath, line_to_words=str.split):
the_file = open(thefilepath)
for line in the_file:
for word in line_to_words(line):
yield word
the_file.close()
for word in words_of_file(thefilepath):
dosomethingwith(word)
The best I could come up with:
def words_of_file(thefilepath, line_to_words=str.split):
the_file = open(thefilepath)
for line in the_file:
for word in line_to_words(line):
yield word
the_file.close()
len(list(words_of_file(thefilepath)))
But that seems clunky.
解决方案BartlebyScrivener <rp*******@gmail.com> wrote:
Still new. I am trying to make a simple word count script.
I found this in the great Python Cookbook, which allows me to process
every word in a file. But how do I use it to count the items generated?
def words_of_file(thefilepath, line_to_words=str.split):
the_file = open(thefilepath)
for line in the_file:
for word in line_to_words(line):
yield word
the_file.close()
for word in words_of_file(thefilepath):
dosomethingwith(word)
The best I could come up with:
def words_of_file(thefilepath, line_to_words=str.split):
the_file = open(thefilepath)
for line in the_file:
for word in line_to_words(line):
yield word
the_file.close()
len(list(words_of_file(thefilepath)))
But that seems clunky.
My preference would be (with the original definition for
words_of_the_file) to code
numwords = sum(1 for w in words_of_the_file(thefilepath))
Alex
BartlebyScrivener wrote:
Still new. I am trying to make a simple word count script.
I found this in the great Python Cookbook, which allows me to process
every word in a file. But how do I use it to count the items generated?
def words_of_file(thefilepath, line_to_words=str.split):
the_file = open(thefilepath)
for line in the_file:
for word in line_to_words(line):
yield word
the_file.close()
for word in words_of_file(thefilepath):
dosomethingwith(word)
The best I could come up with:
def words_of_file(thefilepath, line_to_words=str.split):
the_file = open(thefilepath)
for line in the_file:
for word in line_to_words(line):
yield word
the_file.close()
len(list(words_of_file(thefilepath)))
But that seems clunky.
As clunky as it seems, I don''t think you can beat it in terms of
brevity; if you care about memory efficiency though, here''s what I use:
def length(iterable):
try: return len(iterable)
except:
i = 0
for x in iterable: i += 1
return i
You can even shadow the builtin len() if you prefer:
import __builtin__
def len(iterable):
try: return __builtin__.len(iterable)
except:
i = 0
for x in iterable: i += 1
return i
HTH,
George
Thanks! And thanks for the Cookbook.
rd
"There is no abstract art. You must always start with something.
Afterward you can remove all traces of reality."--Pablo Picasso
这篇关于计算发电机中的物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!