>>> c = 'A/B,C/D,E/F'
>>> [a for b in c.split(',') for (a,_) in b.split('/')]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
ValueError: need more than 1 value to unpack
预期结果是['A', 'C', 'E']
。
这是我期望的方式,但显然它在 Python 中是从头到尾:
>>> [a for (a, _) in b.split('/') for b in c.split(',')]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
最佳答案
您失败的原因是 b.split('/')
没有产生二元组。 双重列表推导意味着您希望将笛卡尔积视为平面流而不是矩阵。即:
>>> [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']
即使您使用嵌套列表理解,您也会得到笛卡尔积 (3x2=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')
关于python - 这个嵌套列表理解有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8291771/