Python dictionary creation syntax

前端 未结 7 1829
时光取名叫无心
时光取名叫无心 2020-12-30 18:25

I\'m wondering if there\'s any way to populate a dictionary such that you have multiple keys mapping to the same value that\'s less verbose than say:

d = {1:         


        
7条回答
  •  执念已碎
    2020-12-30 18:55

    dict((x, {4: 'no'}.get(x, 'yes')) for x in range(1, 5))
    

    Or in 3.x:

    {x: {4: 'no'}.get(x, 'yes') for x in range(1, 5)}
    

提交回复
热议问题