根据搜索条件从 Python 数组中返回随机元素
如果这很简单,我很抱歉,但我已经找了一段时间了,找不到一个简单、有效的解决方案。
我有一个二维 Python 列表列表,仅包含 1 和 0。
例如:
a=[[0,1,0],[0,1,1],[1,0,1]]
我希望随机返回= 1 的随机元素的索引。在这种情况下,我想返回:
[0,1], [1,1], [1,2], [2,0], or [2,2]
以相等的概率。
我可以迭代结构中的每个元素并编译符合条件的索引列表,然后使用 random.choice(list) 随机选择一个 - 但这看起来很慢,我不禁觉得有一种更简洁、更 Pythonic 的方式来解决这个问题。我将为一个 20x20 的数组执行此操作,并且需要执行多次,因此我可以尽可能高效地执行此操作。
预先感谢您的任何帮助和建议!
Apologies if this is straightforward, but I've been looking for a little while now and can't find a simple, efficient solution.
I have a two-dimensional Python list of lists which only consists of 1's and 0's.
e.g.:
a=[[0,1,0],[0,1,1],[1,0,1]]
I wish to return, at random, the indices of a random element which is = 1. In this case I would like to return either:
[0,1], [1,1], [1,2], [2,0], or [2,2]
with an equal probability.
I could iterate through every element in the structure and compile a list of eligible indices and then choose one at random using random.choice(list) - but this seems very slow and I can't help feeling there is a neater, more Pythonic way to approach this. I will be doing this for probably a 20x20 array and will need to do it many times, so I could do with it being as efficient as possible.
Thanks in advance for any help and advice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将使用列表理解来生成元组列表(位置为 1),然后使用 random.choice :
I'd use a list comprehension to generate a list of tuples (positions of 1), then random.choice :
random.choice
允许我们选择一个从列表中随机选择一个元素,因此我们只需要使用列表理解来创建一个元素为 1 的索引列表,然后随机选择一个。我们可以使用以下列表推导式:
这意味着我们可以这样做:
如果您要多次执行此操作,则可能值得缓存由推导式生成的索引列表,然后从中多次选取,而不是每次都计算列表推导式单次。
random.choice
allow us to pick an element at random from a list, so we just need to use a list comprehension to create a list of the indexes where the elements are 1 and then pick one at random.We can use the follow list comprehension:
Which means we can do:
If you will be doing this many times it may be worth caching the list of indexes generated by the comprehension and then picking from it several times, rather than calculating the list comprehension every single time.
我将使用 NumPy 数组来实现此目的:
如果您从一开始就将数据存储在 NumPy 数组中,则此方法可能比使用列表列表执行的任何操作都要快。
如果您想为相同的数据选择多个索引,还有更快的方法。
I would use a NumPy array to achieve this:
If your store your data in NumPy arrays right from the beginning, this approach will probably be faster than anything you can do with a list of lists.
If you want do choose many indices for the same data, there are even faster approaches.
当您从 random.choice 获得结果时,检查它是否是您想要的正确元素(如果它不是随机的)
编辑:以避免与 re 模块混淆,谢谢
When you get your result from random.choice check if it is how you would like it with the correct elements if it is not random again
Edit: to avoid confusion with re module, thanks
另一个想法是以完全不同的方式存储数据:使用一组代表 1 的条目的索引对,而不是列表的列表。在您的示例中,这将是 要
随机选择一个索引对,请使用
To将条目设置为 1,使用
将条目设置为 0,使用
要翻转条目,使用
要检查条目是否为 1,使用
Another idea would be to store the data in a completely different way: Instead of a list of lists, use a set of index pairs representing the entries that are 1. In your example, this would be
To randomly choose an index pair, use
To set an entry to 1, use
To set an entry to 0, use
To flip an entry, use
To check if an entry is 1, use