作者solsiso (solsiso)
看板Python
标题[问题] 新手请教关於回圈中break的问题
时间Tue Apr 1 17:54:26 2014
各位前辈好,想请教以下问题
1.下面程式中有加break和无加break时,在最後print出来的结果会差1,
这会是break导致的吗?还是我的资料有问题?
2.在第2个for loop中,我使用string0去取得list的物件,之後再利用string0以移除
字串中所有的空格及\t。
但为什麽我不能直接用j.replace(' ', '').replace('\t', '')来做移除的动作呢?
3.第2个for loop中的 if判断式,我连续使用and及使用()
我对这判断式的想法是:
执行方式是由左而右,先找第一个and并对它左右二个括号内的式子判断,然後再
做第二个and并对它左右进行判断,若皆为真,才执行if内的statement。
请问这种做法会不会因为short-circuit evaluation而导致我的判断逻辑有误呢?
以下是code部份
code目的是为了判断一堆档案的内容是否符合需要
第一个for loop是读取全部档案
第二个for loop是判断每一个档案是否符合条件
最後印出结果
def holding(hrlist):
count0 = 0
holding = []
for i in hrlist:
file = open( dir_hr + i, 'r' )
content = file.readlines()
file.close()
for j in content:
string0 = j
j = string0.replace(' ', '').replace('\t', '')
if ( 'X' in j.upper() ) and ( '13FHOLDING' in j.upper() ) and (
'REPORT' in j.upper() ):
#holding.append(i)
print j
count0 +=1
break
#'13F HOLDINGS REPORT.' in j.upper() or
print count0
这看起来有点麻烦,还请各位前辈多多包涵,能给我点提示 ~感谢~
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.115.223.82
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Python/M.1396346071.A.3B0.html