作者:
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
這看起來有點麻煩,還請各位前輩多多包涵,能給我點提示 ~感謝~