Python 列表推导式乘积
我正在尝试构建一个特定的列表推导式,用来从一个列表的列表(也就是矩阵)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 个回答
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的乘积是一样的。