Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I wanted to produce lst_new such that,

items = (.1, .5, .9)
lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

lst_new == [[[.1, 2, 3], [.4, 5, 6], [.7, 8, 9]], [[.5, 2, 3], [2, 5, 6], [3.5, 8, 9]], [[.9, 2, 3], [3.6, 5, 6], [6.3, 8, 9]]]

Using list comprehension,

lst_new = [x[0] * i for i in items for x in lst]

But obviously it doesn't work as intended. Help?

question from:https://stackoverflow.com/questions/65930733/modify-nested-list-elements-using-list-comprehension

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
454 views
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...