Converting a list to a set changes element order

后端 未结 11 1133
攒了一身酷
攒了一身酷 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:06

    remove duplicates and preserve order by below function

    def unique(sequence):
        seen = set()
        return [x for x in sequence if not (x in seen or seen.add(x))]
    

    check this link

提交回复
热议问题