Here is an example of the problem I'm trying to solve:
d=[1,2,3]
e=[d[0],d[1]]
def add_up(x,y):
return (x + y)
My attempt:
h=[[add_up(a,b)] for a in e for b in d]
print h
output from my attempt
[[2], [3], [4], [3], [4], [5]]
My desired output: **[[2,3,4],[3,4,5]]**
Any suggestions? Thanks.
您快到了,使用:
print [[add_up(a, b) for b in d] for a in e] # prints [[2, 3, 4], [3, 4, 5]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.