Python Dictionary Comprehension

前端 未结 8 1343
Happy的楠姐
Happy的楠姐 2020-11-21 13:10

Is it possible to create a dictionary comprehension in Python (for the keys)?

Without list comprehensions, you can use something like this:

l = []
fo         


        
8条回答
  •  一个人的身影
    2020-11-21 14:09

    Use dict() on a list of tuples, this solution will allow you to have arbitrary values in each list, so long as they are the same length

    i_s = range(1, 11)
    x_s = range(1, 11)
    # x_s = range(11, 1, -1) # Also works
    d = dict([(i_s[index], x_s[index], ) for index in range(len(i_s))])
    

提交回复
热议问题