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 方法,它返回给定集合中的平方值。
print(str({x*x for x in {1,2,3,4,5}}))
值是正确的,但与提交的值不同,这是为什么呢?
{16, 1, 4, 25, 9}
您创建了一个集合,而不是一个列表,并且集合元素的呈现顺序是任意的。
相比:
>>> print([x*x for x in [1,2,3,4,5]]) [1, 4, 9, 16, 25]