Python的succint語法通過其電池允許冗長的代碼行以可讀的一行表示。請看下面的例子帶有Itertools的等效嵌套循環結構
====================================================|
for a in range(3): |
for b in range(3): |
for c in range(3): |
print (a,b,c), |
- - - - - - - - - - - - - - - - - -|
for e in product(range(3), repeat=3): |
print e, |
====================================================|
for a in range(3): |
for b in range(a , 3): |
for c in range(b , 3): |
print (a,b,c), |
- - - - - - - - - - - - - - - - - -|
for e in combinations_with_replacement(range(3), 3):|
print e, |
====================================================|
for a in range(3): |
for b in range(a + 1, 3): |
for c in range(b + 1, 3): |
print (a,b,c), |
- - - - - - - - - - - - - - - - - -|
for e in combinations(range(3), 3): |
print e, |
====================================================|
for a in range(3): |
for b in range(3): |
for c in range(3): |
if len(set([a,b,c])) == 3: |
print (a,b,c), |
- - - - - - - - - - - - - - - - - -|
for e in permutations(range(3)): |
print e, |
====================================================|
後期我結束了一個深嵌套依賴環路我試圖簡潔表達,但未能
循環的結構將是如下
for a in A():
for b in B(a):
for c in C(b):
foo(a,b,c)
這樣的結構可以用等價的itertools符號表示嗎?
只是一個說明。 Prolog中的最後一個循環將表示爲:a(A),b(A,B),c(B,C)'。如果你喜歡玩循環,請使用Prolog。 – liori 2013-02-23 04:56:13
你在最裏面的循環體中使用'a','b'(中間值)嗎? – jfs 2013-02-23 04:57:15
@liori:Prolog總是引誘我,但從來沒有聰明到學習人工智能 – Abhijit 2013-02-23 04:57:49