python列表理解产品

时间:2011-10-03 17:22:11

标签: python list list-comprehension

我正在尝试构建一个特定的列表理解,从列表/矩阵A和矩阵B列表中获取数据以生成第三个矩阵C.我已经注释了代码并提供了预期的结果,但是正如所写的,列表理解不能达到预期的效果,可以用比我在这里尝试的更优雅的代码表达。 Xt =转置矩阵X,Xs =排序矩阵X

# matrix A with row headings and values
A = [('Apple',0.95,0.99,0.89,0.87,0.93),
('Bear',0.33,0.25.0.85,0.44,0.33),
('Crab',0.55,0.55,0.10,0.43,0.22)]

#matrix B with row headings and values
B = [('Apple',1.00.0.99,1.00,0.95,0.99),
('Bear',0.99,0.99,0.99,0.99,0.99),
('Crab', 0.05,0.19,1.00,0.55,0.89)]

#transpose matrix A and B
At=zip(*A)
Bt=zip(*B)

#generate a new empty matrix, C, and give it the same heading labels as A and B
Ct=At[0:1]

#delete the heading labels on transposed matrices A and B
del At[0]
del Bt[0]

#List Comprehension Code
#multiply all the numbers in row [x] (i.e., apples) for matrix A = apple product A
#multiply all the numbers in row [x] for matrix B = apple product B
#A matrix row [x] product / B matrix row [x] product B = apple value for matrix C
#append apple value under apple heading
#do this for bear rows and crab rows, too
Ct.append((prod(x) for x in zip(*At))/(prod(x) for x in zip(*Bt)))

#untranspose matrix C to match the heading, value configuration of original matrices
C=zip(*Ct)

#sort matrix C rows in descending order based on comprehension [i][1] values
Cs = matrix(sorted(C, key=lambda item: item[1], reverse=True))

基于矩阵A和B的当前值,矩阵C的最终输出应如下所示:

'Apple' 0.7191   #(matrix A apple row product = 0.6696 / matrix B apple row product = 0.9311)
'Crab'  0.6170   #(matrix A crab row product = 0.0029 / matrix B cat row product = 0.0047)
'Bear'  0.0098   #(matrix A bear row product = 0.0093 / matrix B bear row product = 0.9510)

1 个答案:

答案 0 :(得分:1)

我不确定我理解你的要求,可能会遗漏一些东西。

您发布的代码充满了语法错误和未定义的函数。

然而,这就是我认为你要求的,但结果不同:

>>> from operator import mul
>>> A = [('Apple',0.95,0.99,0.89,0.87,0.93),('Bear',0.33,0.25,0.85,0.44,0.33),('Crab',0.55,0.55,0.10,0.43,0.22)]
>>> B = [('Apple',1.00,0.99,1.00,0.95,0.99),('Bear',0.99,0.99,0.99,0.99,0.99),('Crab', 0.05,0.19,1.00,0.55,0.89)]
>>> C = [ (a[0],reduce(mul,a[1:])/reduce(mul,b[1:])) for (a,b) in zip(A,B) ]
>>> C
[('Apple', 0.72737272727272728), ('Bear', 0.010706894358222457), ('Crab', 0.6153755174452985)]

<强>另外

或者,如果我完全放弃矩阵B并且C'Apple'行等于:。 0.95 * 0.99 * 0.89 * 0.87 * 0.83?

>>> prodA = [ (a[0],reduce(mul,a[1:])) for a in A ]
>>> prodA
[('Apple', 0.67725310950000006), ('Bear', 0.010182150000000001), ('Crab', 0.0028616500000000003)]

我如何修改它以获得每个A元素除以其配对B元素的每一行的乘积?即,C'Apple'行等于:
[('Apple',(0.95 / 1.00)*(0.99 / 0.99)*(0.89 / 1.00)*(0.87 / 0.95)*(0.93 / 0.99)]

>>> A_by_B = [ [x[0][0]]+[i/j for (i,j) in x[1:]] for x in [ zip(a,b) for (a,b) in zip(A,B) ]]
>>> A_by_B
[
 ['Apple', 0.94999999999999996, 1.0, 0.89000000000000001, 0.9157894736842106, 0.93939393939393945],
 ['Bear', 0.33333333333333337, 0.25252525252525254, 0.85858585858585856, 0.44444444444444448, 0.33333333333333337],
 ['Crab', 11.0, 2.8947368421052633, 0.10000000000000001, 0.78181818181818175, 0.24719101123595505]
]
>>> prod_A_by_B = [ (x[0],reduce(mul,x[1:])) for x in A_by_B ]
>>> prod_A_by_B
[('Apple', 0.72737272727272728), ('Bear', 0.010706894358222457), ('Crab', 0.61537551744529861)]

每行A的每个值除以B的每个值,然后是每行的乘积,与A的每一行的乘积除以B的每一行的乘积相同。