避免Python代码redux中的代码重复
这是先前问题的后续。我为此得到了一些很好的建议,所以我想我会再试试运气。
from itertools import takewhile
if K is None:
illuminacond = lambda x: x.split(',')[0] != '[Controls]'
else:
illuminacond = lambda x: x.split(',')[0] != '[Controls]' and i < K
af=open('a')
bf=open('b', 'w')
cf=open('c', 'w')
i = 0
if K is None:
for line in takewhile(illuminacond, af):
line_split=line.split(',')
pid=line_split[1][0:3]
out = line_split[1] + ',' + line_split[2] + ',' + line_split[3][1] + line_split[3][3] + ',' \
+ line_split[15] + ',' + line_split[9] + ',' + line_split[10]
if pid!='cnv' and pid!='hCV' and pid!='cnv':
i = i+1
bf.write(out.strip('"')+'\n')
cf.write(line)
else:
for line in takewhile(illuminacond, af):
line_split=line.split(',')
pid=line_split[1][0:3]
out = line_split[1] + ',' + line_split[2] + ',' + line_split[3][1] + line_split[3][3] + ',' \
+ line_split[15] + ',' + line_split[9] + ',' + line_split[10]
if pid!='cnv' and pid!='hCV' and pid!='cnv':
i = i+1
bf.write(out.strip('"')+'\n')
是否可以压缩这段代码?如果我在这样的两个循环中有一些共同点, 一种明显的可能性是只提取通用代码,但是在这里,eww。 烦人的是,这里唯一的区别是写入 c
。
代码简要摘要:如果K
不是None,则循环a
的K
行并写入b
> 和c
。否则,循环遍历所有 a
并仅写入 b
。
This is a followup to an earlier question. I got some good suggestions for that, so I thought I would try my luck again.
from itertools import takewhile
if K is None:
illuminacond = lambda x: x.split(',')[0] != '[Controls]'
else:
illuminacond = lambda x: x.split(',')[0] != '[Controls]' and i < K
af=open('a')
bf=open('b', 'w')
cf=open('c', 'w')
i = 0
if K is None:
for line in takewhile(illuminacond, af):
line_split=line.split(',')
pid=line_split[1][0:3]
out = line_split[1] + ',' + line_split[2] + ',' + line_split[3][1] + line_split[3][3] + ',' \
+ line_split[15] + ',' + line_split[9] + ',' + line_split[10]
if pid!='cnv' and pid!='hCV' and pid!='cnv':
i = i+1
bf.write(out.strip('"')+'\n')
cf.write(line)
else:
for line in takewhile(illuminacond, af):
line_split=line.split(',')
pid=line_split[1][0:3]
out = line_split[1] + ',' + line_split[2] + ',' + line_split[3][1] + line_split[3][3] + ',' \
+ line_split[15] + ',' + line_split[9] + ',' + line_split[10]
if pid!='cnv' and pid!='hCV' and pid!='cnv':
i = i+1
bf.write(out.strip('"')+'\n')
Is it possible to compactify this code? If I have some stuff in common in two loops like this,
one obvious possibility is to just factor out the common code, but here, eww.
The annoying thing is that the only difference here is the writing to c
.
Brief summary of code: If K
is not None, then loop over K
lines of a
and write to both b
and c
. Otherwise, loop over all of a
and just write to b
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不只使用一个循环,而是在该循环内包含条件?另外,我认为您可以消除 lambda 中的冗余。
Why not use only one loop, but including the condition inside that loop? Also, you can get rid of the redundancy in that lambda, I think.
一项检查,一次循环,无类,心理可优化。
One check, one loop, no classes, psyco-optimizable.
为什么不只是:
Why not just:
这个(基于第二类的版本)怎么样?
原始版本:
How about this (second class based version)?
Original version: