[Tutor] Multiple Simultaneous Loops

2005-09-15 Thread Ed Singleton
I roughly want to be able to do:

for f, x in bunch_of_files, range(z):

so that x iterates through my files, and y iterates through something else.

Is this something I can do?

If so, what would be the best way to create a range of indeterminate length?

If not, is there a nice way I can do it, rather than than incrementing
a variable (x = x + 1) every loop?

Or maybe can I access the number of times the loop has run?  ('x = x +
1' is so common there must be some more attractive shortcut).

So far in learning Python I've founbd that when I feel you should be
able to do something, then you can.  This seems a pretty intuitive
thing to want to do, so I'm sure it must be possible, but I can't find
anything on it.

Many thanks

Ed
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multiple Simultaneous Loops

2005-09-15 Thread Pujo Aji
assume:
you have two list with the same size
L1 = [1,2,3]
L2 = [11,22,33]
 
you can zip the L1 and L2 into L
L = zip(L1,L2)  # L = [(1,11),(2,22),(3,33)] 
 
then you can process:
for x in L:
  dosomething(x[0])...
  dosomething(x[1])...
 
I'm not so sure about your problem but
If you want to do something parallel processing then you should go to threading or pyro.
 
Cheers,
pujo
On 9/15/05, Ed Singleton <[EMAIL PROTECTED]> wrote:
I roughly want to be able to do:for f, x in bunch_of_files, range(z):so that x iterates through my files, and y iterates through something else.
Is this something I can do?If so, what would be the best way to create a range of indeterminate length?If not, is there a nice way I can do it, rather than than incrementinga variable (x = x + 1) every loop?
Or maybe can I access the number of times the loop has run?  ('x = x +1' is so common there must be some more attractive shortcut).So far in learning Python I've founbd that when I feel you should be
able to do something, then you can.  This seems a pretty intuitivething to want to do, so I'm sure it must be possible, but I can't findanything on it.Many thanksEd___
Tutor maillist  -  Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multiple Simultaneous Loops

2005-09-15 Thread Andre Engels
On 9/15/05, Ed Singleton <[EMAIL PROTECTED]> wrote:
> I roughly want to be able to do:
> 
> for x, y in bunch_of_files, range(z):
> 
> so that x iterates through my files, and y iterates through something else.
> 
> Is this something I can do?

It's not fully clear to me what you want to do. Do you want to go
through each pair x,y with x in bunch_of_files and y in range(z)? Then
you can do:

for x in bunch_of_files:
for y in range(z):

Or do you want to have one value of y for each value of x? In that
case I think you'd want:
for y in range(len(bunch_of_files)):
x = bunch_of_files[y]

> If so, what would be the best way to create a range of indeterminate length?

I don't think such a thing exists. Either it has some length or it
does not. There's nothing in between.

> If not, is there a nice way I can do it, rather than than incrementing
> a variable (x = x + 1) every loop?
> 
> Or maybe can I access the number of times the loop has run?  ('x = x +
> 1' is so common there must be some more attractive shortcut).

See my second example above?

Andre Engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multiple Simultaneous Loops

2005-09-15 Thread Pierre Barbier de Reuille
You have a much simpler solution !
As this is a most common task to iterate on a sequence while keeping
track of the index, there is an object just for that :

for i,x in enumerate(iterable):
  # Here "i" is the index and "x" the element

Also, to get some "advance" iteration schemes, have a lot at the module
"itertools" ... there is a bunch of very usefull items in it (the most
usefull to me bzing "izip") !

Pierre

Pujo Aji a écrit :
> assume:
> you have two list with the same size
> L1 = [1,2,3]
> L2 = [11,22,33]
>  you can zip the L1 and L2 into L
> L = zip(L1,L2) # L = [(1,11),(2,22),(3,33)] 
>  then you can process:
> for x in L:
>  dosomething(x[0])...
>  dosomething(x[1])...
>  I'm not so sure about your problem but
> If you want to do something parallel processing then you should go to 
> threading or pyro.
>  Cheers,
> pujo
> 
> 

-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77fax   : (33) 4 67 61 56 68
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multiple Simultaneous Loops

2005-09-15 Thread Kent Johnson
Ed Singleton wrote:
> I roughly want to be able to do:
> 
> for f, x in bunch_of_files, range(z):
> 
> so that x iterates through my files, and y iterates through something else.
> 
> Is this something I can do?

In the general case use zip():
for f, x in zip(bunch_of_files, range(z)):

In this case, where the second item is just the index to the loop, use 
enumerate() instead of range() and zip()
for x, f in enumerate(bunch_of_files):

> If so, what would be the best way to create a range of indeterminate length?

itertools.count() generates an "unlimited" sequence.
 
> If not, is there a nice way I can do it, rather than than incrementing
> a variable (x = x + 1) every loop?
> 
> Or maybe can I access the number of times the loop has run?  ('x = x +
> 1' is so common there must be some more attractive shortcut).

enumerate()
 
> So far in learning Python I've founbd that when I feel you should be
> able to do something, then you can.

Yep :-)

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multiple Simultaneous Loops

2005-09-15 Thread Ed Singleton
Wonderful, thank you all of you.

zip, enumerate, and count seem to do everything I want, though I do think

for f, x in bunch_of_files, range(z):

is a little more intuitive than

for f, x in zip(bunch_of_files, range(z)):

Thanks

Ed

On 15/09/05, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Ed Singleton wrote:
> > I roughly want to be able to do:
> >
> > for f, x in bunch_of_files, range(z):
> >
> > so that x iterates through my files, and y iterates through something else.
> >
> > Is this something I can do?
> 
> In the general case use zip():
> for f, x in zip(bunch_of_files, range(z)):
> 
> In this case, where the second item is just the index to the loop, use 
> enumerate() instead of range() and zip()
> for x, f in enumerate(bunch_of_files):
> 
> > If so, what would be the best way to create a range of indeterminate length?
> 
> itertools.count() generates an "unlimited" sequence.
> 
> > If not, is there a nice way I can do it, rather than than incrementing
> > a variable (x = x + 1) every loop?
> >
> > Or maybe can I access the number of times the loop has run?  ('x = x +
> > 1' is so common there must be some more attractive shortcut).
> 
> enumerate()
> 
> > So far in learning Python I've founbd that when I feel you should be
> > able to do something, then you can.
> 
> Yep :-)
> 
> Kent
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Aschenputtel problem

2005-09-15 Thread Christopher Arndt
Hi,

I wonder if there is a shorter form of the following idiom:

list1 = []
list2 = []
for item in original_list:
if condition(item):
list1.append(item)
else:
list2.append(item)

(optional step:)

original_list[:] = list1


I call this the "Aschenputtel" problem, because it is described by the famous
quote from the fairy tale as told by the Grimm brothers:

"Die guten ins Töpfchen, die schlechten ins Kröpfchen."

(The good ones in the pot, the bad ones in the crop)

Chris




signature.asc
Description: OpenPGP digital signature
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] running scripts with windows

2005-09-15 Thread Alan G
If you are a Unix head using windows run to the cygwin site
and install the whole caboodle.

You'll think you are back in Unix land...

If thats impossible them open a DOS boc (aka Command Prompt)
and type

python foo.py

Or even just double click the file in Windows explorer...

Alan G.


- Original Message - 
From: "Ed Hotchkiss" <[EMAIL PROTECTED]>
To: 
Sent: Wednesday, September 14, 2005 10:58 PM
Subject: [Tutor] running scripts with windows


Hi everyone, I'm home for the next few weeks while I'm learning 
Python, and
I'm not on a *NIX box, I'm using windows. How the hell do I run a 
Python
script? Sorry if this is a 'dumb question'.

-- 
edward hotchkiss

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] running scripts with windows

2005-09-15 Thread Alan G
> Cygwin (http://www.cygwin.com/)  It gives you a bash shell in 
> Windows.

Actually it gives you a whole Unix environment including X Windows
and over 500 unix command line tools plus GNU C, sendmail, etc etc...

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multiple Simultaneous Loops

2005-09-15 Thread Alan G
> for f, x in bunch_of_files, range(z):

...
> Or maybe can I access the number of times the 
> loop has run?  

I think thats what enumerate does...


>>> for x,y in enumerate([1,3,5]):
...   print x,y
...
0 1
1 3
2 5
>>>


Yep, looks like what you need.

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Kent Johnson
Christopher Arndt wrote:
> Hi,
> 
> I wonder if there is a shorter form of the following idiom:
> 
> list1 = []
> list2 = []
> for item in original_list:
> if condition(item):
> list1.append(item)
> else:
> list2.append(item)

I don't think so. You can write it as two list comprehensions which is shorter 
but it iterates the original list twice:
list1 = [item for item in original_list if condition(item)]
list2 = [item for item in original_list if not condition(item)]

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] GETTING AN _.EXE VERSION OF _.PY program to run on a machine that does not have Python installed on it. (fwd)

2005-09-15 Thread Danny Yoo


-- Forwarded message --
Date: Wed, 14 Sep 2005 21:33:08 -0500
From: JackA <[EMAIL PROTECTED]>
To: Danny Yoo <[EMAIL PROTECTED]>
Subject: Re: [Tutor] GETTING AN _.EXE VERSION OF _.PY program to run on a
machine that does not have Python installed on it.

Sorry for the boo boo but am learning and will remember.  No .DOC's.

With change of "Windows"  to "Console"  It is working as required.  Now that
the need to get it working is out of the way, I will have to take a better
look at Python.  From what I have seen in the on line information in the
course of doing this, I conclude seems to be an efficient lauguage.  I'l
have to see if I can find a couple of good books on it.

Thanks again and Jason is also very appreciative of all the help recieved.
Jack

- Original Message -
From: "Danny Yoo" <[EMAIL PROTECTED]>
To: "JackA" <[EMAIL PROTECTED]>
Cc: 
Sent: Tuesday, September 13, 2005 12:47 PM
Subject: Re: [Tutor] GETTING AN _.EXE VERSION OF _.PY program to run on a
machine that does not have Python installed on it.


>
>
> On Mon, 12 Sep 2005, JackA wrote:
>
>> I am attaching a word document copy of this EMail which might be easier
>> to read and follow if your EMail window rearanges things to much.
>
> Hi Jack,
>
> Actually, please don't do that.  *grin*
>
> Word documents are actually not really usable in a programming language
> forum.  You're assuming that the recipients are using systems that are
> capable of running Microsoft Word.  You might be surprised, but this is
> not necessarily true.  For maximum utility, please stick to plain text
> formats on Python-Tutor.  The page:
>
>http://expita.com/nomime.html
>
> gives some more reasons for staying with plain text, at least on technical
> mailing lists.
>
>
> Anyway, to your question:
>
>> I have created PROG.EXE from PROG.PY , but it appears NOT to be portable
>> in that when I try to run PROG.EXE on my wife's machine which does not
>> have Python installed on it I get a " See the logfile PROG.EXE log for
>> details".  The logfile has the following ;
>>
>>
>> Traceback (most recent call last):
>>   File "PROG.PY", line 364, in ?
>>   File "PROG.PY", line 320, in Main
>> EOFError: EOF when reading a line
>
>
> Ok, I have an idea of what's going on here.  Let's take a look at your
> 'setup.py' file.
>
>
>
>> The 6 lines of PyToExe.PY used to make PROG.EXE , is shown below
>> starting with ### in line #1.
>>
>> ###  PyToExe.PY = file name._.PY > _.EXE
>>
>> from distutils.core import setup
>> import py2exe
>>
>> setup(windows=['PROG.PY'])
>^^^
>
> Ah.  Change the line in your setup from using 'windows' to 'console'.
>
>
> You're not making a graphical "Windows" program, but one that depends on
> the console.  Technially, a Microsoft Windows GUI program has no console,
> and no access to standard input or output.  That's why your program
> breaks: it's unable to use raw_input(), since that's explicitely meant to
> talk to standard input.
>
>
> So, instead, your setup.py should look like:
>
> ##
> from distutils.core import setup
> import py2exe
>
> setup(console=['PROG.PY'])
> ##
>
>
> Best of wishes to you.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] GETTING AN _.EXE VERSION OF _.PY program to run on a machine that does not have Python installed on it. (fwd)

2005-09-15 Thread Christopher Arndt
Danny Yoo schrieb:
> 
> -- Forwarded message --
> Date: Wed, 14 Sep 2005 21:33:08 -0500
> From: JackA <[EMAIL PROTECTED]>
> To: Danny Yoo <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] GETTING AN _.EXE VERSION OF _.PY program to run on a
> machine that does not have Python installed on it.
> 
> Sorry for the boo boo but am learning and will remember.  No .DOC's.

Another gentle advice: Don't use ALL CAPS WRITING in the subject of your
emails. That's the equivalent to shouting in the online world and people will
just think you're a jerk (which is probably not true but people will think it
nevertheless).

Chris
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Alan Gauld
> I wonder if there is a shorter form of the following idiom:

Bearing in mind that shorter is not necessarily better...

[condition(i) and list1.append(i) or list2.append(i) for i in 
original]

This returns a list of booleans that we throw away but the
list1,list2 containers will have been modified as required.

But the original code(below) is probably more reliable and 
clearer!

> list1 = []
> list2 = []
> for item in original_list:
>if condition(item):
>list1.append(item)
>else:
>list2.append(item)

> I call this the "Aschenputtel" problem, because it is described
> by the famous quote from the fairy tale as told by the
> Grimm brothers:
>
> "Die guten ins Töpfchen, die schlechten ins Kröpfchen."
> (The good ones in the pot, the bad ones in the crop)

HTH,

Alan G. 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] running scripts with windows

2005-09-15 Thread R. Alan Monroe
>> Cygwin (http://www.cygwin.com/)  It gives you a bash shell in 
>> Windows.

> Actually it gives you a whole Unix environment including X Windows
> and over 500 unix command line tools plus GNU C, sendmail, etc etc...

Watch out, because cygwin-native python can conflict with win32
python, if they're both in your path. Buddy of mine had various .py
files not work because of this.

Alan

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Kent Johnson
Alan Gauld wrote:
>>I wonder if there is a shorter form of the following idiom:
> 
> 
> Bearing in mind that shorter is not necessarily better...
> 
> [condition(i) and list1.append(i) or list2.append(i) for i in 
> original]

Hmm, no, this will always evaluate list2.append(i) because the value of 
list1.append(i) is None. You could use

[ (condition(i) and list1 or list2).append(i) for i in original ]

or

for i in original:
  (condition(i) and list1 or list2).append(i)

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] running scripts with windows...slightly OT

2005-09-15 Thread George Flaherty
Yeah be careful with some of the cygwin tools. I had an issue with the jar 
command being picked up from cygwin/bin instead of JAVA_HOME and it was 
corrupting the contents of a jar file.

-george

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of R. Alan Monroe
Sent: Thursday, September 15, 2005 5:24 PM
To: tutor@python.org
Subject: Re: [Tutor] running scripts with windows

>> Cygwin (http://www.cygwin.com/)  It gives you a bash shell in 
>> Windows.

> Actually it gives you a whole Unix environment including X Windows and 
> over 500 unix command line tools plus GNU C, sendmail, etc etc...

Watch out, because cygwin-native python can conflict with win32 python, if 
they're both in your path. Buddy of mine had various .py files not work because 
of this.

Alan

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Roel Schroeven
Alan Gauld schreef:
>>I wonder if there is a shorter form of the following idiom:
> 
> 
> Bearing in mind that shorter is not necessarily better...
> 
> [condition(i) and list1.append(i) or list2.append(i) for i in 
> original]

Alas, won't work. This is one of the cases where the ... and ... or ...
idiom doesn't work; this is why I don't like it and don't use it. The
problem is that list1.append(i) returns None, so list2.append(i) is
always called, even if list1.append(i) was already called:

>>> list1 = []
>>> list2 = []
>>> False and list1.append(2) or list2.append(2)
>>> list1, list2
([], [2])
>>> True and list1.append(4) or list2.append(4)
>>> list1, list2
([4], [2, 4])


-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Please help with comparision in range of numbers

2005-09-15 Thread Srinivas Iyyer
Hello group,

I have a data like this in tab-delim text.

1427021_s_atchr7:102786983-102794499(+)
1452285_a_atchr7:102786983-102794499(+)
1445553_at  chr7:102848766-102910961(-)
1420925_at  chr7:102863841-102887410(+)
1450041_a_atchr7:102863841-102887410(+)
1447553_x_atchr7:102899711-10285(-)
1418478_at  chr7:102991513-103023463(-)
1452892_at  chr7:103132162-103291925(-)
1430157_at  chr7:103292083-103297768(+)



First column represents the identifier. 
Second column representes the position of the
identifier on genome. Because many people might not
understand genome, lets consider it as a sequence
composed of A, T , G and C with lengths of thousands
of thousands of ATGCs. 

The analogy is something like:

Jill am driving from washington DC to Miami on I-95
South. 
Jack is driving from Miami to DC on I-95 North.

If we scale the I-95 South as (-) (negative direction)
from 0-10 units (arbitary) and similarly, I-95
North as (+) (positive direction) again from 0-500,000
arbitarary units. 

My aims is to find if Jack and Jill happen to be in
Florence, south carolina then coordinates (considering
that they are apart 1 or 2 miles or units distance)
then they would be something like :
Jackchr7:102863841-102887410(+)
Jillchr7:102886711-10285(-)


Then they are falling in the same range of coordinates
(1028,63841-1028,87410)(1028,86711-1028,5)
(between 86711 and 87410).

I want to find such instances of jack and jill's that
fall in the same ranges and they should be in opposite
to each other. 

Truely, I always flunk and my mind gets blackedout
when I see these number ranges.  I do not know how to
write : check "if a range of numbers fall in the other
range of numbers" condition. 

tutors could any one help me please. 

thank u

srini




__ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Aschenputtel problem

2005-09-15 Thread John Fouhy
On 16/09/05, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Alan Gauld wrote:
> > Bearing in mind that shorter is not necessarily better...
> >
> > [condition(i) and list1.append(i) or list2.append(i) for i in
> > original]
> 
> Hmm, no, this will always evaluate list2.append(i) because the value of 
> list1.append(i) is None. You could use
> 
> [ (condition(i) and list1 or list2).append(i) for i in original ]

This will not work either, because initially, list1 will be [] which
is false.  So everything will end up in list2.

>>> list1, list2 = [], []
>>> [(i%2==1 and list1 or list2).append(i) for i in range(10)]
[None, None, None, None, None, None, None, None, None, None]
>>> list1, list2
([], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

You can get around this with a dodge..

>>> list1, list2 = [], []
>>> [(i%2 == 1 and [list1] or [list2])[0].append(i) for i in range(10)]
[None, None, None, None, None, None, None, None, None, None]
>>> list1, list2
([1, 3, 5, 7, 9], [0, 2, 4, 6, 8])

But I'm sure that using side-effects in a list comprehension is
prohibited by at least one major religion, so really, I would just
stick to a for loop :-)

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Aschenputtel problem - Shorter is not better?

2005-09-15 Thread Terry Kemmerer







"Bearing in mind that shorter is not necessarily better..."

Wouldn't shorter, as a rule of thumb, as in less language statements, mean fewer 
executions, and therefore faster?





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Alan G
>> [condition(i) and list1.append(i) or list2.append(i) for i in 
>> original]
> 
> Hmm, no, this will always evaluate list2.append(i) because 
> the value of list1.append(i) is None. You could use
> 
> [ (condition(i) and list1 or list2).append(i) for i in original ]

Oops! Good point, which emphasises the point about shorter not 
necessarily being better!

My personal approach would be two separate comprehensions(*) which 
keeps the intent very clear and keeps it short at the expense 
of iterating the original list twice.

(*) Or you could use the filter funtion twice if you want to 
make the intent even more explicit...

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Please help with comparision in range of numbers

2005-09-15 Thread Danny Yoo


> 1427021_s_at  chr7:102786983-102794499(+)
> 1452285_a_at  chr7:102786983-102794499(+)
> 1445553_atchr7:102848766-102910961(-)
> 1420925_atchr7:102863841-102887410(+)
> 1450041_a_at  chr7:102863841-102887410(+)
> 1447553_x_at  chr7:102899711-10285(-)
> 1418478_atchr7:102991513-103023463(-)
> 1452892_atchr7:103132162-103291925(-)
> 1430157_atchr7:103292083-103297768(+)

[some text cut]

> I want to find such instances of jack and jill's that fall in the same
> ranges and they should be in opposite to each other.


Hi Srinivas,


Let's break this down into two separate problems:

1.  Finding Ranges that overlap.
2.  Given two Ranges, see if they're in opposite directions.

I'm going to ignore problem 2 at the moment.  *grin*


If we don't care too much about efficiency, the following should help you
solve problem 1.

##
class Range(object):
def __init__(self, low, high):
assert low <= high
self.low, self.high = low, high

def is_overlapping(self, other):
"""Returns true if this range overlaps the other range.
The exact boolean condition is copied from the
classic textbook "Introduction to Algorithms, 2nd edition",
section 14.3.
"""
return (self.low <= other.high) and other.low <= self.high
##


For example:

##
>>> Range(1, 2).is_overlapping(Range(2, 3))
True
>>> Range(1, 2).is_overlapping(Range(3, 3))
False
>>> Range(1, 2).is_overlapping(Range(-2, 0))
False
>>> Range(1, 2).is_overlapping(Range(-42, 42))
True
##

So if you have sequence of Ranges like this, you can brute force and just
loop across all possible pairs, and do this is_overlapping() check.  You
may want to test this on a small sample dataset just to see that it works.

This approach, however, is slow, and gets slower as the dataset grows.
If we do care about efficiency, we have to do something a little smarter.

Interval trees or K-d Tree data structures are examples of data structures
that can help you do range-intersection queries much more quickly than a
brute-force approach.  There's an implementation of K-d trees in
biopython:

http://biopython.org/docs/api/public/Bio.KDTree-module.html

so you may want to investigate this with the Biopython folks if speed is a
concern.  John Bentley wrote a nice survey article talking about these
range-searching algorithms here:

http://portal.acm.org/citation.cfm?id=356797

although you might need an ACM account to get at the article.



> Truely, I always flunk and my mind gets blackedout when I see these
> number ranges.  I do not know how to write : check "if a range of
> numbers fall in the other range of numbers" condition.

Yeah, me too.  Check out the book reference in the code above: it helps to
clear things up about intervals and range checking.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Christopher Arndt
Alan G schrieb:
> My personal approach would be two separate comprehensions(*) which 
> keeps the intent very clear and keeps it short at the expense 
> of iterating the original list twice.

Of course this works only if condition(item) yields the same result each time
it is called with the same input (which should be the common case).

Also, iterating twice over the list could be expensive if the condition(item)
call takes comparatively long.

Chris
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Boa-Constructor

2005-09-15 Thread Luis N
 >  Luis,
>  
>  I was actually asking how usable Boa-Constructor is right now for project
> purposes, since I had "heard" it was kind of unstable and crashes a lot. Is
> that your experience with Boa-Constructor? Or was the information I found
> misleading?
>  
>  Thanks,
>  Terry 

I found Boa-constructor interesting/useful. Since this was sometime
ago, and wxPython has since gained a few version numbers, I can't
comment on its current stability. It was fine when I tried it, little
bloated though.

I'm a committed emacs user now.

Luis
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Aschenputtel problem

2005-09-15 Thread Bob Gailer
At 10:12 AM 9/15/2005, Christopher Arndt wrote:
Hi,I wonder if there is a 
shorter form of the following idiom:list1 = []list2 = []for item 
in original_list:    if condition(item):    
list1.append(item)    else:    
list2.append(item)Consider (5 lines instead of 7):lists 
= [[], []]for item in original_list:      
lists[condition(item)].append(item)list1 = lists[0]list2 = 
lists[1]This assumes condition() returns 0 or 1 (True)or if you 
don't mind the result in sets (assumes unique elements):set1 = set([x 
for x in original_list if cond(x)])set2 = original_list - set1 


I can't send mail my usual way due to unknown problems. So am using gmail. Please relpy as always to [EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor