Ordering in Python (2.4) dictionary

后端 未结 4 1570
情歌与酒
情歌与酒 2021-01-28 06:33
    r_dict={\'answer1\': \"value1\",\'answer11\': \"value11\",\'answer2\': \"value2\",\'answer3\': \"value3\",\'answer4\': \"value4\",}

    for i in r_dict:
        if(         


        
4条回答
  •  情话喂你
    2021-01-28 07:32

    Dictionaries are unordered - that is, they do have some order, but it's influenced in nonobvious ways by the order of insertion and the hash of the keys. However, there is another implementation that remembers the order of insertion, collections.OrderedDict.

    Edit: For Python 2.4, there are several third party implementations. I haven't used any, but since the one from voidspace looks promising.

提交回复
热议问题