你失敗的原因是b.split('/')
沒有產生一個2元組。 雙列表理解意味着你想把笛卡爾產品當作平面流而不是矩陣。那就是:
>>> [x+'/'+y for y in 'ab' for x in '012']
['0/a', '1/a', '2/a', '0/b', '1/b', '2/b']
# desire output 0,1,2
# not output 0,1,2,0,1,2
你是不是在找6個回答,您正在尋找3.你想要的是:
>>> [frac.split('/')[0] for frac in c.split(',')]
['A', 'C', 'E']
即使你使用了嵌套列表理解,你會得到你笛卡爾乘積(3×2 = 6),並意識到你有重複的信息(你不需要X2):
>>> [[x+'/'+y for y in 'ab'] for x in '012']
[['0/a', '0/b'], ['1/a', '1/b'], ['2/a', '2/b']]
# desire output 0,1,2
# not [0,0],[1,1],[2,2]
以下是做同樣事情的等效方法。儘管我比較了這種比較中生成器和列表之間的主要區別。以列表形式
笛卡爾乘積:
((a,b,c) for a in A for b in B for c in C)
#SAME AS#
((a,b,c) for (a,b,c) in itertools.product(A,B,C))
#SAME AS#
for a in A:
for b in B:
for c in C:
yield (a,b,c)
在矩陣形式的笛卡爾乘積:
[[[(a,b,c) for a in A] for b in B] for c in C]
#SAME AS#
def fC(c):
def fB(b,c):
def fA(a,b,c):
return (a,b,c)
yield [f(a,b,c) for a in A]
yield [fB(b,c) for b in B]
[fC(c) for c in C]
#SAME AS#
Cs = []
for c in C:
Bs = []
for b in B:
As = []
for a in A:
As += [a]
Bs += [As]
Cs += [Bs]
return Cs
重複的功能應用到列表
({'z':z} for x in ({'y':y} for y in ({'x':x} for x in 'abc')))
#SAME AS#
for x in 'abc':
x2 = {'x':x}
y2 = {'y':x2}
z2 = {'z':y2}
yield z2
#SAME AS#
def f(x):
return {'z':{'y':{'x':x}}}
return [f(x) for x in 'abc'] # or map(f,'abc')
以供將來參考,您可以在內部生成器周圍放置'(...)'以避免創建不必要的中間名單,而不是'[...]' – ninjagecko
謝謝你將來會記錄下...... :) – avasal