undefinedfix
Sign in

In Python set derivation formula, why are the results of command line and method call different

ePumps edited in Thu, 24 Nov 2022

集合推导式.png

Analysis: the result of X in range (1100) is 1-99, which should be displayed in sequence. As indicated by the number 2, how can it be printed out with print()? Please give me your problem description

3 Replies
Dafe
commented on Thu, 24 Nov 2022
  1. Set is out of order
  2. Two sets in a graph are equal
  3. Please stop using Python 2
  4. I've tested in Python 3.8 without this problem, and the results are the first
lnjlb
commented on Fri, 25 Nov 2022

The set itself is out of order. Why should we consider sorting.

Adonis95
commented on Fri, 25 Nov 2022

Set is out of order

m = {x for x in range(1,100) if x%9 == 0}
print(m,type(m)) 

m = [x for x in range(1,100) if x%9 == 0]
print(m,type(m))

output

(set([99, 36, 72, 9, 45, 81, 18, 54, 90, 27, 63]), <type 'set'>)
([9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99], <type 'list'>)
  • sourcePython 3.6.5 standard library document 4.9 set type - set, frozenset

The second figure is not clear what the reason is, it should be a special phenomenon in the jupyter environment