简体   繁体   中英

Set function and decimal number

Can someone help me to understand the relationship between the set function and decimal numbers?

For instance:

print(set([1.104 , 2.1 , 3.25 , 4.45 , 5.553 , 6 , 7 , 8]))

gives this output:

{4.45, 3.25, 6, 7, 8, 2.1, 1.104, 5.553}

From the documentation :

A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. (For other containers see the built-in dict, list, and tuple classes, and the collections module.)... Being an unordered collection, sets do not record element position or order of insertion.

So you should not assume (even though it may happen) that its implementation iterates over the elements in the order you put them in.

Also notice that the elements are distinct, so calling set([1, 2, 2, 3, 4, 4]) will return a set {1,2,3,4}.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM