python3 中 set 进行列表去重时 为什么会自动进行排序(升序)

2018-09-06 21:15:39 +08:00
 mili8908
In [35]: set([1, 3, 8, -2, 99, 98, 77, 1, 5, 3, 77, 12])
Out[35]: {-2, 1, 3, 5, 8, 12, 77, 98, 99}


In [31]: o = [2,5,1,2,4,6,0,33,5.4,6.4]

In [32]: c = set(o)

In [33]: c
Out[33]: {0, 1, 2, 4, 5, 5.4, 6, 6.4, 33}
4123 次点击
所在节点    Python
3 条回复
GTim
2018-09-06 21:24:40 +08:00
楼主多试一试几次

即使你看到了十次都是有序的,也不要相信,`set` 是无序的 `set` 是无序的 `set` 是无序的
PythonAnswer
2018-09-06 21:32:39 +08:00
楼主多看文档

5.4. Sets

Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.
drawstar
2020-01-02 11:13:19 +08:00
@PythonAnswer 从文档能看出啥?无序的?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/486911

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX