简体   繁体   English

在python中,如何以重复的随机序列提取不同值的(最小)列表?

[英]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. python中 ,我有一个scipy数组(也可以只是一个列表),它是一个很长的随机浮点序列 ,但可能的值只有4个。我不知道这4个值是什么,我需要找到他们。 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: 您可以使用内置的set函数:

>>> 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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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