I was working on a problem which required the use of python's set function to be applied onto strings. This is what I observed which is baffling me. Please do tell me a reason as to why it occurs if anyone does know.
>>> set('123456789')
{'2', '6', '7', '5', '9', '1', '8', '4', '3'}
>>> set('123456789')
{'2', '6', '5', '7', '9', '1', '8', '4', '3'}
>>> set('132457689')
{'2', '6', '5', '7', '9', '1', '8', '4', '3'}
>>> set('122333444455555666666777777788888888999999999')
{'2', '6', '5', '7', '9', '1', '8', '4', '3'}
>>> >>> set('1223334444555556666667777777888888889999999991324354657869874645231232435465768986753514324354657698587647536425312635746587873562413237485769546372641495565364319483726415523648759648573462')
{'2', '6', '5', '7', '9', '1', '8', '4', '3'}
Despite the number of times or the order in which the elements(1-9) in the string are passed, they seem to get arranged in this strange pattern after execution.
So I thought to generalize this and see. Here is what I observed when I applied it on a-z and 0-9 all combined
>>> set('1234567890qwertyuiopasdfghjklzxcvbnm')
{'r', 'p', '9', '8', 'l', 'c', 't', 's', '5', 'g', '0', '4', 'q', 'b', 'a', 'i', 'x', 'w', 'o', 'z', 'n', 'm', '2', '6', 'u', 'j', '7', 'd', '1', 'e', 'v', 'k', '3', 'f', 'y', 'h'}
>>> set('1234567890qwertyuiopasdfghjklzxcvbnm'[::-1])
{'r', 'p', '9', '8', 'c', 'l', 't', 's', '5', 'g', '0', 'q', '4', 'b', 'a', 'i', 'x', 'w', 'o', 'z', 'n', 'm', '2', '6', 'j', 'u', '7', 'd', '1', 'e', 'v', 'k', '3', 'f', 'y', 'h'}
The order comes out to be different. 'c' & 'l' are flipped. '4' & 'q' are flipped. 'j' & 'u' are flipped.
But something even crazier is this
>>> set('1234567890qwertyuiopasdfghjklzxcvbnm') == set('1234567890qwertyuiopasdfghjklzxcvbnm'[::-1])
True
Any information about why this behavior occurs is much appreciated. Thank you.