从python中具有固定数量元素的集合中进行非常快速的采样 [英] Very fast sampling from a set with fixed number of elements in python

查看:430
本文介绍了从python中具有固定数量元素的集合中进行非常快速的采样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从固定大小的集合中随机抽样一个数字,进行一些计算,然后将新数字放回集合中. (需要的样本数量非常大)

I need to sample uniformly at random a number from a set with fixed size, do some calculation, and put the new number back into the set. (The number samples needed is very large)

我试图将数字存储在列表中,并使用random.choice()选择一个元素,将其删除,然后附加新元素.但这太慢了!

I've tried to store the numbers in a list and use random.choice() to pick an element, remove it, and then append the new element. But that's way too slow!

我正在考虑将数字存储在numpy数组中,对索引列表进行采样,并对每个索引执行计算.

I'm thinking to store the numbers in a numpy array, sample a list of indices, and for each index perform the calculation.

  • 有没有更快的方法来完成此过程?

推荐答案

Python列表在内部以数组形式实现(例如Java ArrayList s,C ++ std::vector s等),因此从中间删除元素是相对较慢:所有后续元素都必须重新编制索引. (请参见 http://www.laurentluce.com/posts/python-list-implementation/以获得更多信息.)由于元素的顺序似乎与您无关,因此建议您只使用random.randint(0, len(L) - 1)选择索引i,然后使用L[i] = calculation(L[i])进行更新第i个元素.

Python lists are implemented internally as arrays (like Java ArrayLists, C++ std::vectors, etc.), so removing an element from the middle is relatively slow: all subsequent elements have to be reindexed. (See http://www.laurentluce.com/posts/python-list-implementation/ for more on this.) Since the order of elements doesn't seem to be relevant to you, I'd recommend you just use random.randint(0, len(L) - 1) to choose an index i, then use L[i] = calculation(L[i]) to update the ith element.

这篇关于从python中具有固定数量元素的集合中进行非常快速的采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆