Python: how to convert a dictionary into a subscriptable array?

后端 未结 4 443
终归单人心
终归单人心 2020-12-09 15:52

I know how to convert a dictionary into an list in Python, but somehow when I try to get, say, the sum of the resulting list, I get the error \'dict_values\' object is

4条回答
  •  遥遥无期
    2020-12-09 16:24

    @mgilson is right. A dictionary , by its intrinsic nature, isn't ordered.

    Still you can do this :

    alpha = {"A":1,"B":2,"C":3,"D":4,"E":5}   # dictionary
    my_list = []
    for key,value in alpha.items() :
        my_list.append(value)
    

    You can access your "values" from my_list, but it will not be in order.

提交回复
热议问题