Re: [Tutor] python at midnight

2010-11-17 Thread ingo
On Sun, Nov 14, 2010 at 11:48 PM, Steven D'Aprano  wrote:

>> How to get this functionality added to Python?
>
> Make a feature request on the Python bug tracker:
>
> http://bugs.python.org/

done: http://bugs.python.org/issue10427

i.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Accessing columns of a CSV file

2010-11-17 Thread Hanlie Pretorius
Hi,

I'm reading a CSV file and I want to test the contents of its second
column before I write it to an output file:

>>> import csv
>>> time_list=['00:00', '03:00', 
>>> '06:00','09:00','12:00','15:00','18:00','21:00']
>>> readfile='C8R004_flow.csv'
>>> in_text=open(readfile,'rb')
>>> cr=csv.reader(in_text)
>>> cr
<_csv.reader object at 0x7f4dfc3b2360>
>>> for row in cr:
... print row
['2005/01/31', '21:00:00', '26.508']
['2005/01/31', '21:12:00', '26.508']
['2005/01/31', '21:24:00', '26.508']
['2005/01/31', '21:36:00', '26.508']
['2005/01/31', '21:48:00', '26.508']
['2005/01/31', '22:00:00', '26.508']
['2005/01/31', '22:12:00', '26.508']
['2005/01/31', '22:24:00', '26.508']
['2005/01/31', '22:36:00', '26.508']
['2005/01/31', '22:48:00', '26.508']
['2005/01/31', '23:00:00', '26.508']
['2005/01/31', '23:12:00', '26.508']
['2005/01/31', '23:24:00', '26.508']
['2005/01/31', '23:36:00', '26.508']
['2005/01/31', '23:48:00', '26.508']
>>>

I would like to test the values in the second column to see if they're
in the list I named time_list and, if so, write the whole row to an
output file.

The problem is that I don't know how to access the second column of
values. I tried the direct route:
>>> for row in cr:
... print cr[1]
...
>>>

and I've tried to convert the csv.reader object into a list:

[code]
>>> file_rows=[]
>>> for row in cr:
... file_rows.append(row)
...
>>> file_rows
[]
[/code]

which clearly doesn't work.

Can someone perhaps suggest a method to access the value in the second
column of each row so that I can test it against the time_list?

Thanks
Hanlie
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing columns of a CSV file

2010-11-17 Thread Adam Bark

On 17/11/10 13:59, Hanlie Pretorius wrote:

Hi,

I'm reading a CSV file and I want to test the contents of its second
column before I write it to an output file:

   

import csv
time_list=['00:00', '03:00', '06:00','09:00','12:00','15:00','18:00','21:00']
readfile='C8R004_flow.csv'
in_text=open(readfile,'rb')
cr=csv.reader(in_text)
cr
 

<_csv.reader object at 0x7f4dfc3b2360>
   

for row in cr:
 

... print row
['2005/01/31', '21:00:00', '26.508']
['2005/01/31', '21:12:00', '26.508']
['2005/01/31', '21:24:00', '26.508']
['2005/01/31', '21:36:00', '26.508']
['2005/01/31', '21:48:00', '26.508']
['2005/01/31', '22:00:00', '26.508']
['2005/01/31', '22:12:00', '26.508']
['2005/01/31', '22:24:00', '26.508']
['2005/01/31', '22:36:00', '26.508']
['2005/01/31', '22:48:00', '26.508']
['2005/01/31', '23:00:00', '26.508']
['2005/01/31', '23:12:00', '26.508']
['2005/01/31', '23:24:00', '26.508']
['2005/01/31', '23:36:00', '26.508']
['2005/01/31', '23:48:00', '26.508']
   
 

I would like to test the values in the second column to see if they're
in the list I named time_list and, if so, write the whole row to an
output file.

The problem is that I don't know how to access the second column of
values. I tried the direct route:
   

for row in cr:
 

... print cr[1]
...
   
You need to print the 2nd item of row not of the entire file ie "print 
row[1]" not "print cr[1]".


HTH.
Adam.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing columns of a CSV file

2010-11-17 Thread Erik H.
Also notice that in time_list you use %H:%M format, while in your rows
the format is %H:%M:%S. Simple string comparison won't work, unless
you add those seconds to your time_list.

Regards,

Erik


On Wed, Nov 17, 2010 at 2:59 PM, Hanlie Pretorius
 wrote:
> Hi,
>
> I'm reading a CSV file and I want to test the contents of its second
> column before I write it to an output file:
>
 import csv
 time_list=['00:00', '03:00', 
 '06:00','09:00','12:00','15:00','18:00','21:00']
 readfile='C8R004_flow.csv'
 in_text=open(readfile,'rb')
 cr=csv.reader(in_text)
 cr
> <_csv.reader object at 0x7f4dfc3b2360>
 for row in cr:
> ...     print row
> ['2005/01/31', '21:00:00', '26.508']
> ['2005/01/31', '21:12:00', '26.508']
> ['2005/01/31', '21:24:00', '26.508']
> ['2005/01/31', '21:36:00', '26.508']
> ['2005/01/31', '21:48:00', '26.508']
> ['2005/01/31', '22:00:00', '26.508']
> ['2005/01/31', '22:12:00', '26.508']
> ['2005/01/31', '22:24:00', '26.508']
> ['2005/01/31', '22:36:00', '26.508']
> ['2005/01/31', '22:48:00', '26.508']
> ['2005/01/31', '23:00:00', '26.508']
> ['2005/01/31', '23:12:00', '26.508']
> ['2005/01/31', '23:24:00', '26.508']
> ['2005/01/31', '23:36:00', '26.508']
> ['2005/01/31', '23:48:00', '26.508']

>
> I would like to test the values in the second column to see if they're
> in the list I named time_list and, if so, write the whole row to an
> output file.
>
> The problem is that I don't know how to access the second column of
> values. I tried the direct route:
 for row in cr:
> ...     print cr[1]
> ...

>
> and I've tried to convert the csv.reader object into a list:
>
> [code]
 file_rows=[]
 for row in cr:
> ...     file_rows.append(row)
> ...
 file_rows
> []
> [/code]
>
> which clearly doesn't work.
>
> Can someone perhaps suggest a method to access the value in the second
> column of each row so that I can test it against the time_list?
>
> Thanks
> Hanlie
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help with script for finding pairs of amicable numbers

2010-11-17 Thread Richard D. Moores
I got no response to my post that began this thread 8 days ago. I
followed that with a new thread, "List comprehension question" that
continued for 60+ posts, and from which I learned a lot -- about
optimizing a function (and the importance of timing the various
candidates for improvement). The function has been radically changed
and speeded up more than 10x. I didn't mention what it was for, but
now here it is in my script to find Amicable Pairs (ta daa!). See
. I've used it to find the
first 195 pairs, or all pairs (n, m), n < m where 1 <= n <=
55,000,000. The list is at .

Now, I have no reason at all to care about Amicable Pairs, but I'm
interested in learning how to make the search for them more efficient.
When I had found all pairs up through n = 34,000,000, I thought I'd
found one way that would find APs faster (by one-third) by eliminating
all odd n's that didn't end in '5' (40% of all n's). In fact, up to n
= 34 million, there are no APs whose n is both odd and whose last
digit is not '5'. So I asked on  if it
is true for all APs: . As you can see, I
should have gone another million out, because that's where the
counterexample (34765731, 36939357) appears. And it remains the only
counterexample up through n = 55 million.

So, Tutors, what can I do to make the search for APs more efficient? C
Smith has pointed out that if I installed sympy, I could do

>>> from sympy import divisors
>>> list(divisors(256))
[1, 2, 4, 8, 16, 32, 64, 128, 256]

and the sum of the proper divisors of 256 would be just
sum(divisors(n)) - n. However, when I did install sympy, and tried
Smith's idea, the result was a major slowdown on my system, for some
unknown reason.

But that's fine, because I wouldn't learn much from sympy anyway,
other than that it performs magic.

I think what I want are ways to eliminate (filter out?) some of the
n's (as in my idea of the odd n's that don't end in '5'). Anyone?

Dick Moores
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Assistance with Psuedocode

2010-11-17 Thread Joe Ohmer
Hello,

The following code works well but I don't understand why the mysteryEffect code 
block changes the picture.
Doesn’t 64*(r/64) just equal r? (Same with g and b.) So this function should 
not change the picture at all. But it does! If anyone can explain how and why 
this actually changes the picture I would appreciate it greatly.


#Creates, duplicates and shows the pic
def main():
  pic1= makePicture( pickAFile() )
  pic2= duplicatePicture( pic1)
  fadeDownFromBlack( pic1 )
  show( pic1 )
  mysteryEffect( pic2 )
  show( pic2 )

#Does a 50% fade of the upper half of the picture
def fadeDownFromBlack( pic1 ):
  w=getWidth(pic1)
  h=getHeight(pic1)
  for y in range(0,h/2):
for x in range(0,w):
  px= getPixel( pic1, x, y )
  setRed(px,y*(2.0/h)*getRed(px))
  setGreen(px,y*(2.0/h)*getGreen(px))
  setBlue(px,y*(2.0/h)*getBlue(px))
repaint( pic1 )

#Adds a rainbow effect to the picture
def mysteryEffect( pic2 ):
  for px in getPixels( pic2 ):
r= getRed ( px )
g= getGreen( px )
b= getBlue( px )
setRed( px, 64*(r/64))
setGreen( px, 64*(g/64))
setBlue( px, 64*(b/64))
  repaint( pic2 )

Thanks,
Joe
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assistance with Psuedocode

2010-11-17 Thread Alan Gauld


"Joe Ohmer"  wrote


The following code works well but I don't understand why
the mysteryEffect code block changes the picture.
Doesn’t 64*(r/64) just equal r?


That dependfs on which version of Python you use.
In  earlier versions '/' meant integer division so

(1/64) * 64=> 0 * 64
(120/64) * 64 => 1 * 64
(128/64) * 64 => 2 * 64
(130/64) * 64 => 2 * 64

So only in the case of exact multiples of 64 does it do
what you expect. However, if you have a recent version
of Python (2.6? onwards) '/' means floating point division
so now you get the result you expect (subject to rounding
error)

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assistance with Psuedocode

2010-11-17 Thread Adam Bark

On 18/11/10 00:49, Alan Gauld wrote:


"Joe Ohmer"  wrote


The following code works well but I don't understand why
the mysteryEffect code block changes the picture.
Doesn’t 64*(r/64) just equal r?


That dependfs on which version of Python you use.
In  earlier versions '/' meant integer division so

(1/64) * 64=> 0 * 64
(120/64) * 64 => 1 * 64
(128/64) * 64 => 2 * 64
(130/64) * 64 => 2 * 64

So only in the case of exact multiples of 64 does it do
what you expect. However, if you have a recent version
of Python (2.6? onwards) '/' means floating point division
so now you get the result you expect (subject to rounding
error)

HTH,

Roughly what I was about to say. 2.6 doesn't do floating point division 
as standard. I think you can import that behaviour.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assistance with Psuedocode

2010-11-17 Thread Dave Angel

On 2:59 PM, Joe Ohmer wrote:

Hello,

The following code works well but I don't understand why the mysteryEffect code 
block changes the picture.
Doesn’t 64*(r/64) just equal r? (Same with g and b.) So this function should 
not change the picture at all. But it does! If anyone can explain how and why 
this actually changes the picture I would appreciate it greatly.



#Adds a rainbow effect to the picture
def mysteryEffect( pic2 ):
   for px in getPixels( pic2 ):
 r= getRed ( px )
 g= getGreen( px )
 b= getBlue( px )
 setRed( px, 64*(r/64))
 setGreen( px, 64*(g/64))
 setBlue( px, 64*(b/64))
   repaint( pic2 )

Thanks,
Joe

You didn't specify the python version.   But I'd guess you're using 2.x 
(as opposed to 3.x), where dividing integers gives an integer by 
default.  To quickly check, try

print  25/9

and if you get 2, you'll see what I mean.  When you then multiply 2 by 
9, you get 18.


DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Books...*

2010-11-17 Thread Srinidhi Rao
Hello Pythoners',

I am a beginner here want to explore python, thinking this is the best place
to start with,
I request you experts to help me become one...

To Start with which is the best book to get a hang of what python is and
also provide some distinction between the Python 2.6 and 3.x...
If this question is relevant can any one suggest which book to refer for the
DataStructures(preferably in C) to have some backdrop.

Kindly help me find a kindle(literary) :)

Thanks and Regards.
|| SRX ||
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor