+ 1
Set(a)
a=[7,2,8,1,9] print(set(a)) Result - [8,1,2,9,7] Why? A thought result [1,2,7,8,9]
23 Answers
+ 1
I think it may have been this: https://www.sololearn.com/Discuss/1556797/which-logic-JUMP_LINK__&&__python__&&__JUMP_LINK-is-using-for-sorting-sets
+ 4
The order in the set depends on the hash value and the hash value for strings is not constant over different runs of the code (other than for integers):
https://code.sololearn.com/cvDIC9X1gpcA/?ref=app
(run this code several times)
+ 3
Yes, it doesn't even give the same result every time. Try running this code 10 times or so
https://code.sololearn.com/cPAtxp9XJ3sX/?ref=app
In the end I still don't know how Python does it.
+ 2
Sets are by definition unordered.
+ 2
Python may have a way of presenting the elements of a set in a specific order, but that's nothing you can to control.
You can not arbitrarily sort a set or create a specific order. You can basically just put stuff in and take it out, and determine if a thing is in it or not.
+ 2
If you want to know this, you'll have to read Python's source code.
It's part of the implementation, we don't need to know how exactly it works.
+ 2
Because sets are not used for ordering.
You have all sorts of other container types for it: lists, tuples, bytearrays etc.
+ 2
Your answers are excellent and I am grateful, but my question is not exhausted. I will look for an answer through experience :) thanks
+ 2
The basis for all containers in cpython are c-arrays which have a fixed length. So every container has a default size and specific rules when and how to expand. We don't notice any of that because it all happens automatically (hail python ;-)).
In a set the values are placed based on their hash value which for integers is just the integer itself. If the hash value/integer is bigger than the default size of the array, it gets distributed using a modulo based mechanism. This is why you should get a kind of ordered set if you only use lower numbers.
It is probably a bit more complicated than that because you also need a way to prevent collisions but that is about the gist of it.
This said I don't really get your output because if I try it here on SL I get the output you expected to get. On what system did you get the other output?
+ 2
Kishalaya Saha Flandre Scarlet
It seems like the order of the set is independant of the length of the list and how many times you call it in a python run instance.
It is however different per run instance.
https://code.sololearn.com/cK0zX6aPDhi4/?ref=app
+ 2
Maybe another way to get a random number.
+ 1
don,t understand :(( why always result for this is single?
+ 1
I believe that this function has alternation logic. The result is always the same for my example. what is the logic ?
+ 1
I understand result is not ordered, but why only one and only so ?
+ 1
Why we dont need?
+ 1
I think there was a long discussion about it quite a while ago, but I can't seem to find it.
Maybe Kishalaya Saha knows?
+ 1
ok. I wil look.later
+ 1
I have the order of this function associated with the address of the cells in memory .. and your links about hash
+ 1
I try on other comps! But i have one result. But in HonFu's link are many so examples. Now i read their experiments. they had different results sometimes
+ 1
Python 3.5 , vista :) 32bit