Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


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

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading