简体   繁体   中英

Python, List Comprhension Multiplying 2D list by 1D list to each elements

I want to multiply a one dimensional list with a two dimensional list elements. How can I do it with list comprehension?

a = [1,2]
b = [[3,4],[5,6]]

The desired result is

c = [[3,8],[5,12]

You can use nested list comprehension:

c = [ [x * y for x, y in zip(a, row)] for row in b ]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM