I was under the impression that set() would order a collection much like .sort()
However it seems that it doesn’t, what was peculiar to me was why it reorders the collection.
>>> h = '321'
>>> set(h)
set(['1', '3', '2'])
>>> h
'321'
>>> h = '22311'
>>> set(h)
set(['1', '3', '2'])
why doesn’t it return set([‘1’, ‘2’, ‘3’]). I also seems that no matter how many instances of each number I user or in what order I use them it always return set([‘1’, ‘3’, ‘2’]). Why?
Edit:
So I have read your answers and my counter to that is this.
>>> l = [1,2,3,3]
>>> set(l)
set([1, 2, 3])
>>> l = [3,3,2,3,1,1,3,2,3]
>>> set(l)
set([1, 2, 3])
Why does it order numbers and not strings?
Also
import random
l = []
for itr in xrange(101):
l.append(random.randint(1,101))
print set(l)
Outputs
>>>
set([1, 2, 4, 5, 6, 8, 10, 11, 12, 14, 15, 16, 18, 19, 23, 24, 25, 26, 29, 30, 31, 32, 34, 40, 43, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 69, 70, 74, 75, 77, 79, 80, 83, 84, 85, 87, 88, 89, 90, 93, 94, 96, 97, 99, 101])
python
setis unordered, hence there is no guarantee that the elements would be ordered in the same way as you specify themIf you want a sorted output, then call sorted:
Responding to your edit: it comes down to the implementation of set. In CPython, it boils down to two things:
1) the set will be sorted by hash (the
__hash__function) modulo a limit2) the limit is generally the next largest power of 2
So let’s look at the int case:
for ints, the hash equals the original value:
Hence, when all the values are ints, it will use the integer hash value and everything works smoothly.
for the string representations, the hashes dont work the same way:
To understand why the sort breaks for [‘1′,’2′,’3’], look at those hash values:
In our example, the mod value is 4 (3 elts, 2^1 = 2, 2^2 = 4) so
Now if you sort this beast, you get the ordering that you see in set: