问题描述
你好,
我想知道这里的人是怎么处理的,因为我经常会遇到这样的事情:
acc = []#accumulator;)
对于fileinput.input()中的行
:
如果条件(行):
如果acc:#1
doSomething(acc)#1
acc = []
else:
acc.append(线)
如果acc:#2
doSomething(acc)#2
BTW我是特别恼火的是#1和#2,因为它是一个狂欢,我认为它很容易出错,你将如何以pythonic的方式做到这一点?
问候
hello,
i''m wondering how people from here handle this, as i often encounter
something like:
acc = [] # accumulator ;)
for line in fileinput.input():
if condition(line):
if acc: #1
doSomething(acc) #1
acc = []
else:
acc.append(line)
if acc: #2
doSomething(acc) #2
BTW i am particularly annoyed by #1 and #2 as it is a reptition, and i
think it is quite error prone, how will you do it in a pythonic way ?
regards
推荐答案
我认为这是最后的if acc:只是一个流结束
隐式标记,而在循环期间,你有明确的标记来表示
信号结束/块开始。没有引入不需要的变量
而且我不知道它是如何容易出错的。
这是我赢得的一个案例''试着把它变成一个衬里,因为它已经非常自然了: - )
I think it is quite ok as the last "if acc:" is just an "end-of-stream"
implicit marker, whereas during the loop, you have explicit markers to
signal end/start of blocks. There is no unwanted variable introduced
and I don''t see how it can be error prone.
This is one of the case I won''t try to make it a one liner, because it
is already very natural :-)
我觉得像itertools.groupby可以让你接近你想要的,
例如,(未经测试)
导入itertools
for condresult,acciter in itertools.groupby(fileinput.imput(),condition):
如果不是condresult:
dosomething(list(acciter))#或dosomething(acciter)如果迭代器可用
IOW,groupy收集条件评估为不同的
值的连续行。假设这是一个只返回两个不同值的函数(对于true
和false,如True和False),那么如果我理解你的程序的逻辑,那么你需要
对实际满足条件的行不执行任何操作,只需触发
作为分隔符并想要处理其他行的非空组,
所以如果不是condresult:应该选择那些。 Groupby不会返回空组AFAIK,
所以你不需要为此测试。此外,你不需要列表中的列表调用(acciter)
如果你的dosomething可以接受迭代器而不是列表。
问候,
Bengt Richter
It looks to me like itertools.groupby could get you close to what you want,
e.g., (untested)
import itertools
for condresult, acciter in itertools.groupby(fileinput.imput(), condition):
if not condresult:
dosomething(list(acciter)) # or dosomething(acciter) if iterator is usable
IOW, groupy collects contiguous lines for which condition evaluates to a distinct
value. Assuming this is a funtion that returns only two distinct values (for true
and false, like True and False), then if I understand your program''s logic, you
do nothing with the line(s) that actually satisfy the condition, you just trigger
on them as delimiters and want to process the nonempty groups of the other lines,
so the "if not condresult:" should select those. Groupby won''t return an empty group AFAIK,
so you don''t need to test for that. Also, you won''t need the list call in list(acciter)
if your dosomething can accept an iterator instead of a list.
Regards,
Bengt Richter
如果doSomething优雅地处理了一个空列表,那么你就是w应该减少
重复:
acc = []
for fileinput.input():
如果条件(行):
doSomething(acc)#1
acc = []
else:
acc.append(行)
doSomething(acc)#2
如果条件足够简单且文件足够小,也许
您可以一次读取整个文件并使用拆分来分隔
件:
contents = file.read ()
for acc in contents.split(" this is the delimiter line \ n" ):
doSomething(acc.split(" \ n"))
(可能有一些重复分隔线或
分隔符行出现在文件的开头或结尾处
上面的代码将不起作用。告诫经理。)
如果条件稍微复杂一点,也许re.split会工作。$ / b
或许你可以看看拆分并看看它做了什么(因为你的代码
在概念上非常类似于它。)
OTOH,FWIW,你的版本非常干净且非常易读且适合我的
脑完美。
HTH,
Dan
-
Dan Sommers
< http://www.tombstonezero.net/dan/>
If doSomething handled an empty list gracefully, then you would have
less repetition:
acc = []
for line in fileinput.input():
if condition(line):
doSomething(acc) #1
acc = []
else:
acc.append(line)
doSomething(acc) #2
If condition were simple enough and the file(s) small enough, perhaps
you could read the whole file at once and use split to separate the
pieces:
contents = file.read()
for acc in contents.split( "this is the delimiter line\n" ):
doSomething(acc.split("\n"))
(There are probably some strange cases of repeated delimiter lines or
delimiter lines occurring at the beginning or end of the file for which
the above code will not work. Caveat emptor.)
If condition were a little more complicated, perhaps re.split would
work.
Or maybe you could look at split and see what it does (since your code
is conceptually very similar to it).
OTOH, FWIW, your version is very clean and very readable and fits my
brain perfectly.
HTH,
Dan
--
Dan Sommers
<http://www.tombstonezero.net/dan/>
这篇关于建议:你如何用acc迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!