+ 4
Why the set1 is sorted and set2 no?
3 ответов
+ 8
https://docs.python.org/3.7/tutorial/datastructures.html#sets
+ 2
Both sets are unsorted (even if it looks differently). Sets are unordered collection of unique elements and cannot be indexed too.
To get set sorted you should use: sorted(), but this function convert set to list.
set1 = {3, 9, 5, 7}
set2 = sorted(set1)
print (set1, type (set1))
print (set2, type (set2))
#output:
# {9, 3, 5, 7} <class 'set'>
# [3, 5, 7, 9] <class 'list'>
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2464/
+ 2
In the first case (set1), the so-called sorting is an accident. Because out of the definition of a set it is a "container" containing non-repeatable elements (unique) in a random order.
As can be seen from the second case (set2) here are the elements of the set, already in random order. (As it should be). There is nothing scary about this, it's a set)
It's a variable data type and almost similar to the list of functions:
1. add elements (add)
2. delete (pop)
3. concatenate, subtract, multiply, divide, etc .