我有一個簡單的if語句在我的代碼如果所謂的聲明,忽略後,即使條件已經滿足
if len(bootstrap_node_list_recieved_no_dups) >= min_node_to_complete_boot_strap:
print "recieved required nodes"
基本上我想知道是否有足夠的節點,我只希望這發生一次,如代碼仍然會繼續運行,因此當前每次運行if語句都會如我所料。
有沒有一種方法來編寫代碼,以便if語句運行,但是一旦它完成後永遠不會再運行?由於輸入不是常數,因此需要>=
。
我希望這很清楚,因爲它有點難以描述。
更新,
我試圖實施的建議,但我得到的錯誤
UnboundLocalError: local variable 'flag' referenced before assignment
下面
全碼:
flag = False
def number_of_duplicates_in_list():
number_recieved = len(bootstrap_node_list_recieved)
bootstrap_node_list_recieved_before = len(bootstrap_node_list_recieved_no_dups)
" this method works in O(n^2) time and is thus very slow on large lists"
for i in bootstrap_node_list_recieved:
if i not in bootstrap_node_list_recieved_no_dups:
bootstrap_node_list_recieved_no_dups.append(i)
assert len(bootstrap_node_list_recieved_no_dups) >= bootstrap_node_list_recieved_before
if len(bootstrap_node_list_recieved_no_dups) >= min_node_to_complete_boot_strap and flag is False:
print "recieved required nodes"
flag = True
'如果hasnt_run_yet和len(...):hasnt_run_yet = False; ...'...? – deceze