List Comprehensions (from Python)

2000-10-08 Thread raptor

hi,
I haven't used Python... but last days I read some stuff, wanted to compare
both languages for myself and found something interesting.
They are proposing extentinon to Pyhon 2 (with their so called PEP
documents, this also is good idea i.e. using current or some modified
version of RFC's for features addition in  Perl 7,8,9,10 ;") )

Can this be done easly at the moment OR via some of the new proposals ?!!!?
Does this have some benefit compared to array creation via cycles  !!!


List Comprehensions
This is a flexible new notation for lists whose elements are computed from
another list (or lists). The simplest form is:

[ for  in ]

For example, [i**2 for i in range(4)] yields the list [0, 1, 4, 9]. This is
more efficient than a for loop with a list.append() call.

You can also add a condition:

[ for  in  if ]

For example, [w for w in words if w == w.lower()] would yield the list of
words that contain no uppercase characters. This is more efficient than a
for loop with an if statement and a list.append() call.

You can also have nested for loops and more than one 'if' clause. For
example, here's a function that flattens a sequence of sequences::

def flatten(seq):
return [x for subseq in seq for x in subseq]

flatten([[0], [1,2,3], [4,5], [6,7,8,9], []])

This prints

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]



thanx
=
iVAN
[EMAIL PROTECTED]
=




Re: List Comprehensions (from Python)

2000-10-08 Thread Simon Cozens

On Sun, Oct 08, 2000 at 01:12:13PM +0100, raptor wrote:
> [ for  in ]
> Can this be done easly at the moment OR via some of the new proposals ?!!!?

map { expression } sequence

-- 
I used to be disgusted, now I find I'm just amused.
-- Elvis Costello



Re: List Comprehensions (from Python)

2000-10-08 Thread Jeremy Howard

Simon Cozens wrote:
> On Sun, Oct 08, 2000 at 01:12:13PM +0100, raptor wrote:
> > [ for  in ]
> > Can this be done easly at the moment OR via some of the new proposals
?!!!?
>
> map { expression } sequence
>
See also RFC 81.