[问题] 新手请教关于循环中break的问题

楼主: solsiso (solsiso)   2014-04-01 17:54:26
各位前辈好,想请教以下问题
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
这看起来有点麻烦,还请各位前辈多多包涵,能给我点提示 ~感谢~

Links booklink

Contact Us: admin [ a t ] ucptt.com