Python Forum
I found weird thing. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: I found weird thing. (/thread-13132.html)



I found weird thing. - catastrophe_K - Sep-29-2018

I use python 3.7.0

if I type this,
a=set('12345')

and then type a,
outcome likes {3,1,2,4,5} or {4,1,2,3,5} or {1,3,4,2,5} in random sequence.

but I found this one.

if I type this
a=set(i for i in range (1,12) if i%2==0)
and then type a,
outcome is always {2,4,6,8,10} why ALWAYS outcome like this?

and one more weird thing,

when I type this
a=set(i for i in range (1,10) if i%2==0)
and then type a,
outcome is always {8,2,4,6} Whatttttttttttttttt

I think print of set-type is always random. is it wrong??


RE: I found weird thing. - gruntfutuk - Sep-29-2018

You seem to have magically converted unicode characters to integers as well :-)

There is no ordering of sets, order (such as it is) is based on hash values. Ordering is not even an implementation detail in CPython (as it was for dictionaries in CPython 3.6 before becoming standard in 3.7).

Thus, there is no weirdness. The output order is whatever it happens to be and should not be relied on.