Zipping a dictionary whose values are lists

2012-04-12 Thread tkpmep
I using Python 3.2 and have a dictionary >>> d = {0:[1,2], 1:[1,2,3], 2:[1,2,3,4]} whose values are lists I would like to zip into a list of tuples. If I explicitly write: >>> list(zip([1,2], [1,2,3], [1,2,3,4]) [(1, 1, 1), (2, 2, 2)] I get exactly what I want. On the other hand, I have tried >

Re: Finding subsets for a robust regression

2008-09-30 Thread tkpmep
Thank you everyone, for your input. The help is much appreciated. Thomas Philips -- http://mail.python.org/mailman/listinfo/python-list

Finding subsets for a robust regression

2008-09-29 Thread tkpmep
I have coded a robust (Theil-Sen) regression routine which takes as inputs two lists of numbers, x and y, and returns a robust estimate of the slope and intercept of the best robust straight line fit. In a pre-processing phase, I create two new lists, x1 and y1; x1 has only the unique values in x,

Detecting the first time I open/append to a file

2008-09-23 Thread tkpmep
I have a simulation that runs many times with different parameters, and I want to aggregate the output into a single file with one rub: I want a header to be written only the first time. My program looks a bit like this: def main(): for param in range(10): simulate(param) def simulat

Re: Identifying the start of good data in a list

2008-08-26 Thread tkpmep
On Aug 26, 7:23 pm, Emile van Sebille <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > I have a list that starts with zeros, has sporadic data, and then has > > good data. I define the point at  which the data turns good to be the > > first index with a non-zero entry that is followed by a

Identifying the start of good data in a list

2008-08-26 Thread tkpmep
I have a list that starts with zeros, has sporadic data, and then has good data. I define the point at which the data turns good to be the first index with a non-zero entry that is followed by at least 4 consecutive non-zero data items (i.e. a week's worth of non-zero data). For example, if my lis

Getting up and running with Python on a Mac

2008-05-29 Thread tkpmep
I've just bought an iMac (OS X 10.5.2, will almost immediately jump to 10.5.3), and am looking to install Python on it, and to use it with XCode, Apple's IDE. Some googling suggests that a number of people have had trouble getting Python to run satisfactorily on their Macs. This is my first Mac, an

Rpy - partially blank R window

2008-04-11 Thread tkpmep
I have just installed R and Rpy, and am experiencing an odd problem when driving R from Python - if I create a plot in R, the portion of the plot window that lies under the IDLE window in which I type my Python code remains blank. So if I type >>> from rpy import * >>> x = range(10) >>> y = [i **

Re: Cannot start RPy - need win32api

2008-04-11 Thread tkpmep
Thanks a mill - works like a charm! -- http://mail.python.org/mailman/listinfo/python-list

Cannot start RPy - need win32api

2008-04-11 Thread tkpmep
I'm running Python 2.5.2 on Windows XP and need to interface with R, so I downloaded the R 2.6.2 statistical package and installed it, and did the same for RPy 1.02 (i made sure I got the version for Python 2.5 and R 2.62.). When I go to the Python command line and type >>> from rpy import * I get

Re: Dispatching functions from a dictionary

2008-03-30 Thread tkpmep
Paul, George, Thanks a mill - the help is greatly appreciated. Thomas Philips -- http://mail.python.org/mailman/listinfo/python-list

Dispatching functions from a dictionary

2008-03-30 Thread tkpmep
To keep a simulation tidy, I created a dispatcher that generates random variables drawn from various distributions as follows: import random RVType = 1 #Type of random variable - pulled from RVDict RVDict= {'1': random.betavariate(1,1), '2': random.expovariate(1), '3': rand

Order in which modules are imported

2008-02-22 Thread tkpmep
I imported two modules (random and matplotlib), and found that the functions available to me from the random module depended on the order in which the imports occured. In particular, if I import random first, none of its functions seem to be available, while if I import it after matplotlib, I seem

int/long bug in locale?

2007-08-28 Thread tkpmep
To pretty up some numbers stored as strings, I used locale to format them with commas. I then found the following error: >>> import locale >>>locale.setlocale(locale.LC_ALL, 'English_United States.1252') 'English_United States.1252' >>> locale.format('%d', float('2244012500.'), grouping = Tr

Re: Downloading multiple csv files from a website

2007-08-17 Thread tkpmep
Mike, Thanks for the pointers. I looked through the ASPN cookbook, but found a more reliable (and easier to implement) way to get the files I want. I downloaded GNU Wget from http://users.ugent.be/~bpuype/wget/( the current version is 1.10.2), and then ran it from Python as follows import os rc =

Re: Downloading multiple csv files from a website

2007-08-17 Thread tkpmep
Our systems administrator suggested that I try wget, a GNU utility that is designed to pick up data. It might prove to be the easiest way to get the data I want, and I am going to try that first. Thanks again. Thomas Philips -- http://mail.python.org/mailman/listinfo/python-list

Downloading multiple csv files from a website

2007-08-17 Thread tkpmep
I'd like to download data from the website http://www.russell.com/Indexes/performance/daily_values_US.asp. On this web page, there are links to a number of .csv files, and I'd like to download all of them automatically each day. The file names are not visible on the page, but if I click on a link,

Re: pyExcelerator bug?

2007-05-21 Thread tkpmep
John, I'd be delighted to try xlwt (does it work under Python 2.4 and 2.5?) I followed the link to ...svn/xlwt/trunk and found a collection of files, but no Windows installer. How do I install xlwt? Thanks in advance Thomas Philips -- http://mail.python.org/mailman/listinfo/python-list

pyExcelerator bug?

2007-05-16 Thread tkpmep
My program creates three lists: the first has dates expressed as strings, the second has floats that are strictly positive, and the third has floats that are strictly negative. I have no trouble writing the data in these lists to a .csv file using the csv module using the following code. outfile =

Re: Behavior of mutable class variables

2007-05-09 Thread tkpmep
Thanks for the insights. I solved the problem as follows: I created a new class method called cleanUp, which resets NStocks to an empty list and N1 to 0. Works like a charm - it's the first time I've used a class method, and I immediately see its utility. Thanks again class Stock(object): NSto

Re: Behavior of mutable class variables

2007-05-09 Thread tkpmep
On May 9, 5:25 pm, [EMAIL PROTECTED] wrote: > To test some theories, I created a new class variable, an int named Diez, Thanks. It is for precisely this reason that I added another class variable - the immutable int N1. But this too keeps getting incremented on subsequent calls to simulation( ). I

Re: Behavior of mutable class variables

2007-05-09 Thread tkpmep
To test some theories, I created a new class variable, an int named N1, which is not mutable. So my Stock class now looks as follows: class Stock(object): NStocks = [] #Class variables N1 = 0 def __init__(self, id, returnHistory): self.id = id self

Behavior of mutable class variables

2007-05-09 Thread tkpmep
I have written a program that runs portfolio simulations with different parameters and prints the output, but am mystified by the behavior of a mutable class variable. A simplified version of the program follows - would you kindly help me understand why it behaves the way it does. The function mai

Re: Generalized range

2007-04-26 Thread tkpmep
Thanks - you have covered a fair bit of gorund here - I will modify myRange taking your suggestions into account. The one suggestion that I'm going to have to think through is repeatedly incrementing res. I deliberately did not use this as repeated addition can cause rounding errors to accumulate,

Generalized range

2007-04-26 Thread tkpmep
I need to create ranges that can start and end with real numbers. Searching this newsgroup brought me to a function that I then modified as follows: def myRange(iMin, iMax=None, iStep=1): """Extends range to real numbers. Wherever possible, use Python's range . In other cases, make the

Using the Python interpreter

2007-04-18 Thread tkpmep
Instead of starting IDLE as I normally do, I started the Python interpreter and tried to run a program. I got a Python prompt (>>>), and then tried unsuccessfully to run a Python script named Script1.py that runs perfectly well in IDLE. Here's what I did: >>>Script1.py Traceback (most recent call

Indentifying the LAST occurrence of an item in a list

2007-04-04 Thread tkpmep
For any list x, x.index(item) returns the index of the FIRST occurrence of the item in x. Is there a simple way to identify the LAST occurrence of an item in a list? My solution feels complex - reverse the list, look for the first occurence of the item in the reversed list, and then subtract its in

Finding the insertion point in a list

2007-03-16 Thread tkpmep
I have an ordered list e.g. x = [0, 100, 200, 1000], and given any positive integer y, I want to determine its appropriate position in the list (i.e the point at which I would have to insert it in order to keep the list sorted. I can clearly do this with a series of if statements: if yx[i] for i i

Re: Globbing files by their creation date

2007-01-17 Thread tkpmep
Thanks a mill - os.path.getctime(f) is what I needed. Unfortunately, my attempts to turn the integer it returns into a date have failed. >>> os.path.getctime(fn)#fn was created today, 1/17/2007 1168955503 I tried to convert this to a date object by typing >>>datetime.date.fromordinal(1168955

Globbing files by their creation date

2007-01-16 Thread tkpmep
I'd like to create a list of all files in a directory that were created after a certain date. How does one do this? I've used glob.glob to create a list of all files whose name matches a substring, but I don't see how I can use it to identify files by their creation date. Thanks in advance for the

Re: list looping error

2006-12-29 Thread tkpmep
What you really want to write is for i in x: for j in i: print j The outer loop iterates over the tuples in the list, while the inner loop iterates over the elements of each tuple. So j (in your example) is always an integer, and is therefore unsubscriptable, which is exactly what the

SciPy Optimization syntax

2006-09-20 Thread tkpmep
I'm trying to optimize a function using SciPy's optimize.fmin, but am clearly getting the syntax wrong, and would be grateful for some guiidance. First, here's the function def func(Y,x): """Y holds samples of a function sampled at t=-3,-2,-1,0,1,2,3. Y[3]=0 always. func return

IronPython and scipy/pyExcelerator

2006-07-20 Thread tkpmep
I'm looking forward to the release IronPython, primarily for its IDE. I currently use scipy and pyExcelerator to crunch numbers and write them to Excel: does can these packages be used with IronPython as well? Thanks in advance Thomas Philips -- http://mail.python.org/mailman/listinfo/python-li

PyExcelerator

2006-06-02 Thread tkpmep
I write data to Excel files using PyExcelerator 0.6.3.a and have done so successfully for small files (10-15 cells). I'm experiencing an error when writing a big chunk of data (10,000 cells) to Excel. By way of comparison, the same data writes perfectly well to a csv file using Python's built in cs

Re: PyExcelerator: How does one close a file?

2006-05-10 Thread tkpmep
John, I had spelled PyExcelerator with a capital P, but it worked just fine. I then checked the directory it was installed in, and found it read C:\Python24\Lib\site-packages\PyExcelerator. As soon as I changed the directory name to C:\Python24\Lib\site-packages\pyExcelerator, my programs stopped

PyExcelerator: How does one close a file?

2006-05-09 Thread tkpmep
I use PyExcelerator to write data into Excel files, and repeatedly call the function writeData with different filenames fn from my main routine. Unfortunately, only one file - the last one - is created. The problem seems to lie with the fact that I do not close the existing file before calling the

Re: Using StopIteration

2006-05-08 Thread tkpmep
This is just what the doctor ordered. Thanks, as always, everyone! > By breaking out of the while loop as shown above. > > Peter -- http://mail.python.org/mailman/listinfo/python-list

Using StopIteration

2006-05-08 Thread tkpmep
I create list of files, open each file in turn, skip past all the blank lines, and then process the first line that starts with a number (see code below) filenames=glob.glob("C:/*.txt") for fn in filenames: f =file(fn) line = " " while line[0] not in digits: line = f.next()

Re: Partially unpacking a sequence

2006-04-06 Thread tkpmep
Thank you, everyone, for resolving my question. At one point, while trying to solve the problem, I typed >>> y[1,3] Traceback (most recent call last): File "", line 1, in ? TypeError: list indices must be integers The error message gave me no clue as to what I was doing wrong (in my mind, I was

Partially unpacking a sequence

2006-04-06 Thread tkpmep
I have a list y >>>y ['20001201', 'ARRO', '04276410', '18.500', '19.500', '18.500', '19.500', '224'] from which I want to extract only the 2nd and 4th item by partially unpacking the list. So I tried >>>a,b = y[2,4] Traceback (most recent call last): File "", line 1, in ? TypeError: list indices

Using PyExcelerator

2006-03-22 Thread tkpmep
I have just installed PyExcelerator, and now want to use it to read Excel spreadsheets with a variable number of rows and columns and with multiple sheets. Unfortunately, no documentation seems to accompany PyExcelerator. Does anyone know of a good source of documentations and/or examples? The auth

Re: Installing PyExcelerator to write Excel files from Python

2006-03-21 Thread tkpmep
Thanks!!! I had a good laugh at myself after i got it working. -- http://mail.python.org/mailman/listinfo/python-list

Installing PyExcelerator to write Excel files from Python

2006-03-21 Thread tkpmep
I downloaded PyExcelerator.zip as I need to write some data into Excel files, and tried unsuccessfully to install it. I unzipped the files into C:/Python24/Lib/site-packages/PyExcelerator, and in a python command line window typed >>>os.chdir("C:/Python24/Lib/site-packages/PyExcelerator") followe

Question about csv writer

2006-03-20 Thread tkpmep
I expected the following code to work: f = file(fn,"wb") writer = csv.writer(f) for i in range(IMax): writer.writerow([dates[i]].append([ReturnHistories[j][i] for j in range(N)])) but instead i got the following error message: Error: sequence expected However, if i modify the code to read w

Initializing a list of lists

2006-03-19 Thread tkpmep
I want to create a list of lists, each of which is identical, but which can be modified independently i.e: >>>x = [ [0], [0], [0] ] >>> x[0].append(1) >>> x [[0, 1], [0], [0]] The above construct works if I have only few items, but if I have many, I'd prefer to write >>> N =3 >>> x =N*[[0]] >>> x

Re: Computing correlations with SciPy

2006-03-19 Thread tkpmep
Tested it and it works like a charm! Thank you very much for fixing this. Not knowing what an SVN is, I simply copied the code into the appropriate library files and it works perfectly well. May I suggest a simple enhancement: modify corrcoef so that if it is fed two 1 dimensional arrays, it retur

Computing correlations with SciPy

2006-03-16 Thread tkpmep
I want to compute the correlation between two sequences X and Y, and tried using SciPy to do so without success.l Here's what I have, how can I correct it? >>> X = [1, 2, 3, 4, 5] >>> Y = [5, 4, 3, 2, 1] >>> import scipy >>> scipy.corrcoef(X,Y) Traceback (most recent call last): File "", line 1,

Getting started with Scipy/NumPy

2006-03-15 Thread tkpmep
I installed SciPy and NumPy (0.9.5, because 0.9.6 does not work with the current version of SciPy), and had some teething troubles. I looked around for help and observed that the tutorial is dated October 2004, and is not as thorough as Python's documentation. Is there an alternative source of info

Module imports

2006-01-30 Thread tkpmep
I have written a number of functions and stored them in a file named myFunctions.py. This keeps all my functions under one roof, and if I want to use or one or more of them in a program, I can start the program off with from myFunctions import f1,f2 Some of these functions require various math ro

Robust statistics and optimmization from Python

2005-08-29 Thread tkpmep
I use Python to generate a huge amount of data in a .csv file which I then process using Excel. In particular, I use Excel's solver to solve a number of non-linear equation, and then regress the results of hundreds of calls to Solver against a set of known values, enabling me to calibrate my model.

Re: Reading just a few lines from a text file

2005-08-23 Thread tkpmep
Right now my code reads as follows: infile=file(FileName) for line in reversed(infile.readlines()): #Search from the bottom up if int(line.split()[0]) == MyDate: Data= float(line.split()[-1]) break infile.close() I have to read about 10,000 files, each with data. I'm l

Reading just a few lines from a text file

2005-08-23 Thread tkpmep
I have a text file with many hundreds of lines of data. The data of interest to me, however, resides at the bottom of the file, in the last 20 lines. Right now, I read the entire file and discard the stuff I don't need. I'd like to speed up my program by reading only the last 20 lines. How do I do

Re: Using properties

2005-05-25 Thread tkpmep
Thanks for the help. I now understand it better. As Bruno points out, I really don't need a property in this case, as my attribute is public, and I will remove it. Thomas Philips -- http://mail.python.org/mailman/listinfo/python-list

Using properties

2005-05-25 Thread tkpmep
I have a class with a name attribute, which I want to modify using property.The following code works just fine: class Portfolio(object): def __init__( self, name="Port1"): self.name=name def getname(self): return self._name def setname(self,newname="Port2"):

Re: Using reverse iteration to clean up a list

2005-03-13 Thread tkpmep
Thank you all so much for the generous dollop of help: the dictionary suggestion is particularly helpful. The problem arises as follows: A software application stores the securities held in a portfolio in a .csv file, one row per security, with three colulmns. The first has a security identifier o

Using reverse iteration to clean up a list

2005-03-12 Thread tkpmep
I have list of lists of the following form L=[['A', 100], ['B', 300], ['A', 400], ['B', -100]] I want to aggregate these lists, i.e. to reduce L to L=[['A', 500], ['B', 200]] #500 = 100+400, 200=300-100 Here's how I have done it: L.sort() for i in range(len(L),0,-1): if L[i-1][0]=L[i][0]:

PythonWin

2005-03-12 Thread tkpmep
I have a Python program that collects user input using msg = "Enter the full path and name of the file to be processed: " answer = raw_input(msg) If I run it in IDLE, the question is splashed across the execution window, and if it is long, simply wraps to the next line. Most importantly, it is in

PythonWin line spacing

2005-03-09 Thread tkpmep
I've just downloaded and installed ActivePython and am trying to customize the PythonWin editor. Its line spacing seems to default to 2, and consequently, I cannot see many lines of code on a screen. How do I set the line spacing to 1? Thomas Philips -- http://mail.python.org/mailman/listinfo/py