Removing items from a list

2012-02-10 Thread Thomas Philips
In the past, when deleting items from a list, I looped through the
list in reverse to avoid accidentally deleting items I wanted to keep.
I tried something different today, and, to my surprise, was able to
delete items correctly, regardless of the direction in which I looped,
in both Python 3.2.2. and 2..1 -  does the remove() function somehow
allow the iteration to continue correctly even when items are removed
from the midde of the list?

>>> x = list(range(10))
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for i in x:
if i % 2 == 0:
x.remove(i)

>>> x
[1, 3, 5, 7, 9]
>>> for i in reversed(x):
if i % 2 == 0:
x.remove(i)

>>> x
[1, 3, 5, 7, 9]
>>> x = list(range(10))
>>> for i in reversed(x):
if i % 2 == 0:
    x.remove(i)


>>> x
[1, 3, 5, 7, 9]

Sincerely

Thomas Philips
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Removing items from a list

2012-02-10 Thread Thomas Philips
Thanks for the insight. I saw the behavious as soon as I extended x
with a bunch of 0's

>>> x = list(range(10))
>>> x.extend([0]*10)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> for i in reversed(x):
if i % 2 == 0:
x.remove(i)

>>> x
[1, 3, 5, 7, 9]

>>> x = list(range(10))
>>> x.extend([0]*10)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> for i in x:
if i % 2 == 0:
x.remove(i)


>>> x
[1, 3, 5, 7, 9, 0, 0, 0, 0, 0]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Removing items from a list

2012-02-13 Thread Thomas Philips
I could indeed have addressed this problem with a list comprehension.
It escaped me at the time because the larger problem I was trying to
solve included removing data from a dictionary:

months =
sorted(list(dataDict.keys()))  #Sort
months in ascending order

for mth in
reversed(months):#Remove
months with inadequate data
if len(dataDict[mth]) < minItems:
   months.remove(mth)
   del dataDict[mth]

There's more than one way to solve this problem, and, with the benefit
of hindsight, my solution was sloppy, but thanks to the help I
received, I was able to get my code working correctly. Cleaning up is
the next phase!

Thanks, all.

-- 
http://mail.python.org/mailman/listinfo/python-list


Teething troubles with Python on a Mac

2008-08-02 Thread Thomas Philips
I've got a new iMac, have installed Python 2.5.2 on it, and am now
trying to run my existing Python programs on it (these are typically
number crunching programs that I run from IDLE). Some things work,
while others don't, and as this is my first time grappling with Unix
and MacOS X, I appreciate some guidance on how to get up and running.
Here are details of my setup interspersed with my woes and my
questions. Your assistance is greatly appreciated.

Item 1. I downloaded and installed 2.5.2 from python.org. Runs fine,
it installed a MacPython directory under applications, which has a few
icons, including IDLE. However, I have absolutely NO idea where all
the Python files are kept.

Question 1: How can I locate the Python installation? There a few
files under Applications > MacPython 2.5, but this is clearly not the
entire installation.

Question 2: In Finder, If I click on Search For > Past week, I can see
a sequence of folders under the horizontal scroll bar at the bottom of
the window, allowing me to determine that some files that were placed
under Applications > MacPython 2.5> Extra> Demo. But I do not seem to
be able to see the sequence of folders under the horizontal scroll bar
in any finder window. What do I need to do to make the folder sequence
visible in all Finder Windows?


Item 2. I downloaded and installed the SciPy superpack from
Macinscience,org. Scipy seems to import correctly into the Python
2.5.1 that comes with the Mac, but i cannot import it into MacPython
2.5.2.

Question 3. What do I have to do in order to make SciPy work with both
Python 2.5.1 (that comes with the Mac) and 2.5.2 (that I installed
myself)?


Item 3. I accidentally clicked on something (not sure what) and
suddenly found the path to the numpy folder was Macintosh HD >
Developer > SDKs > MacOSX10.5.sdk>System>
Library>Frameworks>Python.framework>Versions>2.5>extras>lib>Python>numpy.
This is insane!!! Where would the superpack have installed SciPy, and
how can I find it?

Question 4. How do I get MacPython 2.5.2 to see SciPy, NumPy etc.
Question 5. How can I find the current value of the PYTHONPATH
environment variable, and how can I edit it and add a directory in
which I keep my Python programs.
Question 6. Apparently there's a Property List Editor that must be
used to create a file called ~ /.MacOSX/environment.plist. I can't
find a directory called ~/.MacOSX. Is this a hidden directory?


Item 4. I opened a terminal window, and typed ipython. Here's what I
got:

Welcome to IPython. I will try to create a personal configuration
directory
where you can customize many aspects of IPython's functionality in:

/Users/tom/.ipython
WARNING:
Installation error. IPython's directory was not found.

Check the following:

The ipython/IPython directory should be in a directory belonging to
your
PYTHONPATH environment variable (that is, it should be in a directory
belonging to sys.path). You can copy it explicitly there or just link
to it.

IPython will create a minimal default configuration for you.
Ipython actually runs once I hit enter, but I do not seem to be able
to import and run my programs.

Question 7. What is this cryptic message all about? I'm completely
confused.


I never thought I'd say this, but it actually seemed a lot easier to
get Python and any associated programs up and running on Windows! I
suspect that a large fraction of my troubles are due to the fact that
I am brand new to the Mac and to Unix, but I bought the Mac in part
because I thought that using it was effortless.

Thank you in advance for your help.

Sincerely

Thomas Philips
--
http://mail.python.org/mailman/listinfo/python-list


MatplotLib errors

2008-04-29 Thread Thomas Philips
I have just started using MatPlotLib, and use it to generate graphs
from Python simulations. It often happens that the graph is generated
and a Visual C++ Runtime Library error then pops up: Runtime Error!
Program C:\Pythin25\Pythonw.exe   This application has requested the
Runtime to terminate in an unusual way. Please contact the
application's support team for more information.

I'm running Python 2.5.2 under Windows XP. Any thoughts on what what
may be causing the problem?

Thanks in advance

Thomas Philips
--
http://mail.python.org/mailman/listinfo/python-list


FTP problem

2011-01-14 Thread Thomas Philips
I'm using ftplib for the first time, and am having trouble getting it
to work. I type

>>> from ftplib import FTP
>>> ftp = FTP('ftp.indexftp.barcap.com', 'A Valid Username', ' A Valid 
>>> Password')

where I have suppressed the user name and password, and I get

Traceback (most recent call last):
  File "", line 1, in 
ftp = FTP('ftp.indexftp.barcap.com')
  File "C:\Python26\lib\ftplib.py", line 116, in __init__
self.connect(host)
  File "C:\Python26\lib\ftplib.py", line 131, in connect
self.sock = socket.create_connection((self.host, self.port),
self.timeout)
  File "C:\Python26\lib\socket.py", line 498, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno 11001] getaddrinfo failed

I have tried this on two different computers and on two different
versions of Python (2.6 and 2.7). I get the same error both times, and
have no understanding of what the problem might be. Any assistance
would be greatly appreciated.

Sincerely

Thomas Philips
-- 
http://mail.python.org/mailman/listinfo/python-list


Simple addition to random module - Student's t

2009-09-02 Thread Thomas Philips
While the random module allows one to generate randome numbers with a
variety of distributions, some useful distributions are omitted - the
Student's t being among them. This distribution is easily derived from
the normal distribution and the chi-squared distribution (which in
turn is a special case of the gamma distribution). I edited and tested
a routine to generate random variables with a Student's t distribution
that I found on http://www.johndcook.com/python_student_t_rng.html,
which has  one bug - there is an extra factor of two in y. The
corrected and tested code follows - how does one go about getting this
incorporated into random so that the entire community can beneffit
from it?

Sincerely

Thomas Philips

def student_t(df): # df is the number of degrees of freedom
if df < 2  or int(df) != df:
   raise ValueError, 'student_tvariate: df must be a integer > 1'

x = random.gauss(0, 1)
y = random.gammavariate(df/2.0, 2)

return x / (math.sqrt(y/df))


References:

1. Student's t distribution, including relationship to normal and chi-
squared distributions: http://en.wikipedia.org/wiki/Student's_t-distribution
2. Chi-squared distribution, including relationship to Gamma
distribution: http://en.wikipedia.org/wiki/Chi-square_distribution
3. John Cook's original version (with the extra factor of 2):
http://www.johndcook.com/python_student_t_rng.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple addition to random module - Student's t

2009-09-02 Thread Thomas Philips
On Sep 2, 12:28 pm, Mark Dickinson  wrote:
> On Sep 2, 2:51 pm, Thomas Philips  wrote:
>
> > def student_t(df):         # df is the number of degrees of freedom
> >     if df < 2  or int(df) != df:
> >        raise ValueError, 'student_tvariate: df must be a integer > 1'
>
> By the way, why do you exclude the possibility df=1 here?
>
> --
> Mark

I exclude df=1 hereBecause the variance is then infinite (in fact, the
distribution is then Cauchy). That said, your point is well taken;
allowing df=1 makes the Cauchy distribution available to users of
random, in much the same way as the Gamma makes the Chi-squared
available. Here's the revised code:

def student_tvariate(df): # df is the number of degrees of
freedom
if df < 1  or int(df) != df:
raise ValueError, 'student_tvariate: df must be a positive
integer'

x = random.gauss(0, 1)
y = random.gammavariate(df/2.0, 2)

return x / (math.sqrt(y/df))


I'll follow your suggestion, add in documentation and submit it to
bugs.python.com. Thanks for your guidance.

Thomas Philips

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple addition to random module - Student's t

2009-09-02 Thread Thomas Philips
On Sep 2, 1:03 pm, Thomas Philips  wrote:
> On Sep 2, 12:28 pm, Mark Dickinson  wrote:
>
> > On Sep 2, 2:51 pm, Thomas Philips  wrote:
>
> > > def student_t(df):         # df is the number of degrees of freedom
> > >     if df < 2  or int(df) != df:
> > >        raise ValueError, 'student_tvariate: df must be a integer > 1'
>
> > By the way, why do you exclude the possibility df=1 here?
>
> > --
> > Mark
>
> I exclude df=1 hereBecause the variance is then infinite (in fact, the
> distribution is then Cauchy). That said, your point is well taken;
> allowing df=1 makes the Cauchy distribution available to users of
> random, in much the same way as the Gamma makes the Chi-squared
> available. Here's the revised code:
>
> def student_tvariate(df):         # df is the number of degrees of
> freedom
>     if df < 1  or int(df) != df:
>         raise ValueError, 'student_tvariate: df must be a positive
> integer'
>
>     x = random.gauss(0, 1)
>     y = random.gammavariate(df/2.0, 2)
>
>     return x / (math.sqrt(y/df))
>
> I'll follow your suggestion, add in documentation and submit it to
> bugs.python.com. Thanks for your guidance.
>
> Thomas Philips

Mark,

I mis-spoke - the variance is infinite when df=2 (the variance is df/
(df-2), and you get the Cauchy when df=2. So I'm going to eat humble
pie and go back to

def student_tvariate(df): # df is the number of degrees of
freedom
if df < 2  or int(df) != df:
   raise ValueError, 'student_tvariate: df must be a integer > 1'

x = random.gauss(0, 1)
y = random.gammavariate(df/2.0, 2)

return x / (math.sqrt(y/df))



I made the mistake because the denominator is  equivalent to the
square root of the sample variance of df normal observations, which in
turn has df-1 degrees of freedom...Oh well,  I think it's
much easier to apologize than to rationalize my error


Sincerely

Tom
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple addition to random module - Student's t

2009-09-02 Thread Thomas Philips
On Sep 2, 2:37 pm, Mark Dickinson  wrote:
> On Sep 2, 6:15 pm, Thomas Philips  wrote:
>
> > I mis-spoke - the variance is infinite when df=2 (the variance is df/
> > (df-2),
>
> Yes:  the variance is infinite both for df=2 and df=1, and Student's t
> with df=1 doesn't even have an expectation.  I don't see why this
> would stop you from generating meaningful samples, though.
>
> > and you get the Cauchy when df=2.
>
> Are you sure about this?  All my statistics books are currently hiding
> in my mother-in-law's attic, several hundred miles away, but wikipedia
> and mathworld seem to say that df=1 gives you the Cauchy distribution.
>
> > I made the mistake because the denominator is  equivalent to the
> > square root of the sample variance of df normal observations,
>
> As I'm reading it, the denominator is the square root of the sample
> variance of *df+1* independent standard normal observations.  I agree
> that the wikipedia description is a bit confusing.
>
> It seems that there are uses for Student's t distribution with
> non-integral degrees of freedom.  The Boost library, and the R
> programming language both allow non-integral degrees of freedom.
> So (as Robert Kern already suggested), you could drop the test
> for integrality of df.  In fact, you could just drop the tests
> on df entirely:  df <= 0.0 will be picked up in the gammavariate
> call.
>
> --
> Mark

To tell you the truth, I have never used it with a non-integer number
of degrees of freedom, but that's not the same as saying that df
should be an integer. When df is an integer, one can interpret the t-
distribution as the ratio of a unit normal (i.e. N(0,1)) to the sample
standard deviation of a set of df+1 unit normals divided by sqrt(df
+1). However, as Robert Kern correctly observes, the distribution is
defined for all positive non-integer df, though we then lose the above
interpretation, and must think of it in abstract terms. The
distribution has infinite variance when df=2 and an undefined mean
when df<=1, but the code can still be used to generate samples.
Whether or not these samples make sense is altogether another
question, but it's easy enough to remmove the restrictions.
-- 
http://mail.python.org/mailman/listinfo/python-list