Understanding the set() function

前端 未结 5 778
故里飘歌
故里飘歌 2020-11-29 02:07

In python, set() is an unordered collection with no duplicate elements. However, I am not able to understand how it generates the output.

For example,

5条回答
  •  独厮守ぢ
    2020-11-29 02:28

    As +Volatility and yourself pointed out, sets are unordered. If you need the elements to be in order, just call sorted on the set:

    >>> y = [1, 1, 6, 6, 6, 6, 6, 8, 8]
    >>> sorted(set(y))
    [1, 6, 8]
    

提交回复
热议问题