+ 2

Python SETS missunderstanding

# Hello guys, help me clarify the following problem. # We have the 2 following sets. first = {1, 2, 3, 4, 5, 6} second = {4, 5, 6, 7, 8, 9} # We want to print the difference of second and first set print(second - first) # The printed result will be {8, 9, 7} # My question for you, why is the printed result {8, 9, 7} and not {7, 8, 9} # Thank you

30th Jan 2019, 12:50 PM
Octavian Feodot
Octavian Feodot - avatar
4 Réponses
+ 5
This is because a set is an unordered data structure. This makes sets faster than lists for example. If you need the result ordered you can use: print (sorted (second - first)) It will print a list with the result in crescent order.
30th Jan 2019, 1:17 PM
Pedro Tortello
Pedro Tortello - avatar
+ 2
I understand you. I don't know how to answer to this.. maybe this is something about how the compiler handles the 0s and 1s? I don't know. 7: 00000111 8: 00001000 9: 00001001
30th Jan 2019, 1:38 PM
Pedro Tortello
Pedro Tortello - avatar
+ 1
Thank you for your answer, but it s still unclear for me why the compiler chooses to solve it this way. {7,8,9} would have been faster in my opinion as 7 is the first element from the end of the set which is not in the first set
30th Jan 2019, 1:22 PM
Octavian Feodot
Octavian Feodot - avatar
+ 1
Thanks for your answer, I will look deeper into this topic 👌🏻
30th Jan 2019, 1:46 PM
Octavian Feodot
Octavian Feodot - avatar