从列表中删除的随机样本 [英] Random Sample with remove from List

查看:81
本文介绍了从列表中删除的随机样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据在一个列表中,例如:

I have data in a list like:

L = [(3,4,5),(1,4,5),(1,2,3),(1,2,3)]

我需要随机抽样2个大小,因此我想使用:

I need to sample randomly a size of 2 so I wanted to use:

import random
t1 = random.sample(set(L),2)

现在T1是一个随机提取值的列表,但是我想从我们的初始列表中删除那些从我们的初始列表中随机提取的值.我可以做一个线性的for循环,但是对于这个任务,我试图做一个更大的列表.因此运行时间将永远长久!

Now T1 is a List of the randomly pulled values, But i wanted to remove those randomly pulled from our initial list from our initial list. I could do a linear for loop but for the task I'm trying to do this for a larger list. so the Run time would take for ever!

关于如何执行此操作的任何建议?

Any suggestions on how to go about this?

推荐答案

t1 = [L.pop(random.randrange(len(L))) for _ in xrange(2)]

不会更改L中其余元素的顺序.

doesn't change the order of the remaining elements in L.

这篇关于从列表中删除的随机样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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