LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-18-2015, 08:49 AM   #1
noodow715
LQ Newbie
 
Registered: May 2015
Posts: 5

Rep: Reputation: Disabled
Python: How to select 10 random numbers from a text file n times?


I already know how to take the mean and variance of it afterwards. Any help?
Attached Files
File Type: txt hsp.txt (567 Bytes, 37 views)

Last edited by noodow715; 05-18-2015 at 03:58 PM.
 
Old 05-18-2015, 11:03 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,217

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Did you mean 10 random non-empty lines?

The following will give you each sample as a list of lists of floats.

Code:
import pprint
import random

with open('hsp.txt') as f:
    lines = [l.strip().split('\t') for l in f.readlines() if l.strip()]

for line in lines:
    line[:] = [float(number) for number in line]

# Whatever
n = 2

for i in xrange(1, n + 1):
    print
    print "Try {}".format(i)
    samples = random.sample(lines, 10)

    pprint.pprint(samples)

Last edited by dugan; 05-18-2015 at 11:15 AM.
 
Old 05-18-2015, 07:41 PM   #3
noodow715
LQ Newbie
 
Registered: May 2015
Posts: 5

Original Poster
Rep: Reputation: Disabled
If the txt file contains only one column, is there a way to take the means of each of those 10 "lines"?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Parsing Javascript To JSON Using Python 3 cin_ Programming 3 05-18-2015 04:08 PM
[SOLVED] python convert output to a list simhumcon Linux - Newbie 5 05-06-2014 02:47 AM
Python output MTK358 Programming 4 06-14-2010 08:02 PM
Colored output in Python MTK358 Programming 2 12-24-2009 08:01 PM
Python serial output seems strange edM Programming 2 04-27-2009 04:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration