Skip to content
Advertisement

resolve matrix lists with lambda and map

we have 3 lists in below

    [2,3,4]
    [5,6,7]
    [8,9,10]

so , how we can sum all of similar index in lists together? i mean is 2 and 5 and 8 be sum together & 3 and 6 and 9 also be sum together & 4 and 7 and 10 as well ? but just use lambda and map…

actually i have no idea for that and this code is just for sending this question

    x=[
    [5,8,1],
    [9,4,7],
    [2,6,3],
    ]
    
    
    print(list(map(lambda x : x[1], x)))

Advertisement

Answer

to get them all together use zip:

new = list((zip(*matrix)))

output:

[(2, 5, 8), (3, 6, 9), (4, 7, 10)]

to sum them up you can use sum and map:

sum = list(map(sum, zip(*matrix)))

output:

[15, 18, 21]
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement