下面的兩個代碼示例是一個問題的舊例子,我在其中遍歷數字列表以找到符合條件列表的數字,但找不到表達它的整潔方式的數字爲:上述的刪除冗餘/冷凝代碼
condition1 and condition2 and condition3 and condition4 and condition5
兩個例子:
if str(x).count("2")==0 and str(x).count("4")==0 and str(x).count("6")==0 and str(x).count("8")==0 and str(x).count("0")==0:
if x % 11==0 and x % 12 ==0 and x % 13 ==0 and x%14==0 and x %15 == 0 and x%16==0 and x%17==0 and x%18==0 and x%19==0 and x%20==0:
是否有這樣做的一個簡單的,更簡潔的方式?
我的第一個重試是條件存儲在列表如下圖所示:
list=["2","4","6","8","0"]
for element in list:
#call the function on all elements of the list
list=[11,12,13,14,15,16,17,18,19,20]
for element in list:
#call the function on all elements of the list
但我希望一個更整潔的/簡單的方法。
其實,我是誰問的一個之前關於所有問題的問題,除非它與這個問題無關! –
這就是你的Python--適用於各種各樣問題的簡單工具。 –