Converting a list to a set changes element order

后端 未结 11 1134
攒了一身酷
攒了一身酷 2020-11-21 23:26

Recently I noticed that when I am converting a list to set the order of elements is changed and is sorted by character.

Consider this examp

11条回答
  •  青春惊慌失措
    2020-11-22 00:18

    An implementation of the highest score concept above that brings it back to a list:

    def SetOfListInOrder(incominglist):
        from collections import OrderedDict
        outtemp = OrderedDict()
        for item in incominglist:
            outtemp[item] = None
        return(list(outtemp))
    

    Tested (briefly) on Python 3.6 and Python 2.7.

提交回复
热议问题