简体   繁体   中英

In python, how to extract the (minimal) list of different values in a random sequence with repetitions?

In python , I have a scipy array (can also be just a list), which is a long random sequence of floats, but the possible values are just 4. I don't know which are those 4 values, and I need to find them. What would be the most efficient / elegant way to do that?

I have few ideas (sort the list, take the minimum, pop all the subsequent elements equal to that, take the first non-equal, etc.). Can you come up with something nicer?

Thanks!

You can use the builtin set function:

>>> ar = scipy.array([0.4,0.3,0.4,0.9,0.2,0.3,0.9,0.4,0.4])
>>> set(ar)
set([0.90000000000000002, 0.29999999999999999, 0.20000000000000001, 0.40000000000000002])

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