+ 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]

11th Oct 2019, 7:52 PM
Ivan Ivanov
Ivan Ivanov - avatar
23 Answers
+ 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)
12th Oct 2019, 5:54 AM
Thoq!
Thoq! - avatar
+ 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.
12th Oct 2019, 2:17 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
Sets are by definition unordered.
11th Oct 2019, 7:58 PM
Thoq!
Thoq! - avatar
+ 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.
11th Oct 2019, 8:27 PM
HonFu
HonFu - avatar
+ 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.
11th Oct 2019, 8:33 PM
HonFu
HonFu - avatar
+ 2
Because sets are not used for ordering. You have all sorts of other container types for it: lists, tuples, bytearrays etc.
11th Oct 2019, 8:35 PM
HonFu
HonFu - avatar
+ 2
Your answers are excellent and I am grateful, but my question is not exhausted. I will look for an answer through experience :) thanks
11th Oct 2019, 8:38 PM
Ivan Ivanov
Ivan Ivanov - avatar
+ 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?
11th Oct 2019, 9:13 PM
Thoq!
Thoq! - avatar
+ 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
12th Oct 2019, 4:15 AM
Louis
Louis - avatar
+ 2
Maybe another way to get a random number.
12th Oct 2019, 1:52 PM
PRO
PRO - avatar
+ 1
don,t understand :(( why always result for this is single?
11th Oct 2019, 8:00 PM
Ivan Ivanov
Ivan Ivanov - avatar
+ 1
I believe that this function has alternation logic. The result is always the same for my example. what is the logic ?
11th Oct 2019, 8:30 PM
Ivan Ivanov
Ivan Ivanov - avatar
+ 1
I understand result is not ordered, but why only one and only so ?
11th Oct 2019, 8:31 PM
Ivan Ivanov
Ivan Ivanov - avatar
+ 1
Why we dont need?
11th Oct 2019, 8:34 PM
Ivan Ivanov
Ivan Ivanov - avatar
+ 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?
11th Oct 2019, 8:41 PM
HonFu
HonFu - avatar
+ 1
ok. I wil look.later
11th Oct 2019, 8:42 PM
Ivan Ivanov
Ivan Ivanov - avatar
+ 1
I have the order of this function associated with the address of the cells in memory .. and your links about hash
11th Oct 2019, 8:54 PM
Ivan Ivanov
Ivan Ivanov - avatar
+ 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
11th Oct 2019, 9:17 PM
Ivan Ivanov
Ivan Ivanov - avatar
+ 1
Python 3.5 , vista :) 32bit
11th Oct 2019, 9:21 PM
Ivan Ivanov
Ivan Ivanov - avatar