473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: sampling without replacement

Alexy>But in Python it's very slow...<

I'm the first one to say that CPython is slow, but almost any language
is slow if you use such wrong algorithms like you do.
There are many ways to solve your problem efficiently, one of such
ways, among the simpler ones is to to not modify the original list:
>>from random import shuffle, seed
items = list("abcdefghi jklm")
seed(10)
shuffle(items )
it_items = iter(items)
it_items.next ()
'i'
>>it_items.next ()
'd'
>>it_items.next ()
'l'
>>it_items.next ()
'b'
>>it_items.next ()
'j'
>>it_items.next ()
'a'
>>it_items.next ()
'e'

If you don't want to extract the same element twice across different
runs of the program, then you may create a class like this:

from random import shuffle, seed

class Sampler(object) :
def __init__(self, items, init_seed=1):
self.items = list(items)
self.last = len(self.items) - 1
self.init_seed = init_seed
seed(init_seed)
shuffle(self.it ems)
def __repr__(self):
return repr(self.items[:self.last+1])
def next(self):
if self.last < 0:
raise StopIteration
self.last -= 1
return self.items[self.last+1]
def save(self, filename):
pass
# saves self.last and self.init_seed on disk

samp = Sampler("abcdef ghijklm")
print samp
print samp.next()
print samp.next()
print samp.next()

That class code is raw, you may want to improve it in some ways.

Bye,
bearophile
Jun 27 '08 #1
0 810

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1589
by: didifouke | last post by:
Hi, I try to sample and playback speech using snack. My idea is to have a socket based server that plays back the sound and a client that samples the input from the sound card and sends it to the server using the socket connection. On the snack page there is a tutorial showing a socket server implementation using tcl:...
1
4710
by: steflhermitte | last post by:
Dear cpp-ians, I want to apply a stratified sampling on an image. Following simplified example will explain my problem. The original image I with nrows and ncols is now a vector V of length (nrow x ncol) and every element of the vector contians its pixel value. vector float V ;
1
2094
by: Morgan Cheng | last post by:
Hi, I am curious that how sampling profiler works. How can a program(profiler) know the running detailis of another program? I googled this topic and not much info is got. Is there anybody get some insight about it? Thanks & Regards,
0
1026
by: himanshu | last post by:
Hi i am one problem in my project. I want to do virtual sampling to convert jpg/png file swf. How is it possible? Is there ant tools by which is te possible?
4
3305
by: kenfar | last post by:
I've got a large table on db2 8.2.1 that I rarely perform runstats on. It has about 600 million rows organized in a single MDC time dimension on a non-dpf warehouse. Anyhow, we recently ran runstats on it with a 20% sampling. After this was performed the highly selective queries using the time dimension slowed down drastically (8000%...
0
1055
by: Miloann | last post by:
Is there existing macros or functions that I can perform the following sampling logic? I have a file with 500K transactions. Would like to start with a number and select one transaction every 30 records. The total number of selected transactions are 2000. Thanks.
1
4630
by: helezem | last post by:
pls cud any one help me with a program that generates samples randomly using simple random sampling
1
2397
by: Francogrex | last post by:
Hi, I'm trying to sample without replacement some numbers (xons:70 values out of 1 to 200 and xEPS1cover:150 values out of 1 to 200). Then I'm trying to intersect both samples to see how many are in common. I have tried to write the code below, but I cannot seem to sample without replacement (there are recurrent numbers within the same...
0
941
by: Matei Zaharia | last post by:
I'm looking for a sampling profiler which I can attach to a running Python process without modifying the source code or affecting performance. So far I've only seen references to instrumentation-based profilers. Is any sampling-based tool available?
3
3947
by: wizche | last post by:
Hi Everybody, I'm trying to play a little with waveIn functions... My goal is to calculate if I'm listening a specified sin frequency or not. For this reason I'm going down with the samplerate... How can I be sure of the sampling rate set on my device? If I declare waveFormat.nSamplesPerSec = 9441000; waveInOpen return NO error.. (same if I...
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.