Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我刚刚开始学习 python 集合理解。为什么{ 2**x for x in {0,1,2,3,4} }return{8, 1, 2, 4, 16}而不是 ordered {1, 2, 4, 8, 16}?
{ 2**x for x in {0,1,2,3,4} }
{8, 1, 2, 4, 16}
{1, 2, 4, 8, 16}
从数学上讲,集合没有顺序。当显示或迭代一个集合时,Python 显然需要提供一个特定的顺序,但这个顺序是任意的,不能依赖。但是,对于特定的集合,顺序是固定的;迭代相同的、未修改的集合每次都会产生相同的顺序。