+ 5
Python. Challenge. Set.
x = {1: 0, 0: 1} y = [1, [4,6,5,4], 2] print(list(set(y[x[0]]))[1]) Could you explain me why set(y[x[0]]) is always {4, 5, 6})? How can I find a defined order of unordered collection of items?
5 Respuestas
+ 4
Agreed, it's a really bad challenge question.
The order of the elements in a set is supposed to be arbitrary. And even for integers, it is not always in ascending order. Try print(set([1, 544])) for example.
I think it's report-worthy.
+ 5
X[0]=1
Y[1]=[4,6,5,4]
Set does not same numbers in it
Hence it gives {4,5,6}
+ 1
Kishalaya Saha, thx
According to stackoverflow:
Sets are usually implemented using hash keys (hash codes). So order is probably related to how hash functions are used instead of natural order of its elements.
https://stackoverflow.com/questions/8084825/JUMP_LINK__&&__python__&&__JUMP_LINK-order-in-a-set-of-numbers
0
y[x[0]]
x{0:1, 1:0}
x[0] => 1
So
y[x[0]] => y[1]
y[1] => ?
0
🇪🇪 Tsypa 🇪🇪
Yes, it probably has something to do with hash values, but it's not completely clear. We had a long and confusing discussion a while ago, if you're interested:
https://www.sololearn.com/Discuss/1556797/?ref=app
(Please sort the comments by "Date" and not "Votes").
Either way, we should not be expected to know Python's implementation of the hash function, calculate them for those integers, and find the correct result.