Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
do you master list comprehensions?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 38 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Will Stuyvesant  
View profile  
 More options Dec 13 2004, 3:51 pm
Newsgroups: comp.lang.python
From: "Will Stuyvesant" <hw...@hotmail.com>
Date: 13 Dec 2004 12:51:25 -0800
Local: Mon, Dec 13 2004 3:51 pm
Subject: do you master list comprehensions?
Here is a question about list comprehensions [lc].  The
question is dumb because I can do without [lc]; but I am
posing the question because I am curious.

This:

>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>> result = []
>>> for d in data:

...     for w in d:
...        result.append(w)
>>> print result

['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']

puts all the words in a list, like I want.

How to do this with [lc] instead of for-loops?

I tried funnies like [[w for w in L] for L in data],
that is correct syntax, but you'd never guess.

I know, silly!  No need for [lc]!  So there's my
question.  I am sure a one-liner using [lc] will be very
enlightening.  Like studying LISP.

--
I wish there was a knob on the TV to turn up the intelligence.
There's a knob called `brightness', but it doesn't work.
-- Gallagher


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Max M  
View profile  
 More options Dec 13 2004, 4:07 pm
Newsgroups: comp.lang.python
From: Max M <m...@mxm.dk>
Date: Mon, 13 Dec 2004 22:07:19 +0100
Local: Mon, Dec 13 2004 4:07 pm
Subject: Re: do you master list comprehensions?

Will Stuyvesant wrote:
> I tried funnies like [[w for w in L] for L in data],
> that is correct syntax, but you'd never guess.

That is absolutely correct. It's not a funnie at all. If you find it odd
it's only because you are not used to list comprehensiones.

In that case you might be more comfortable with:

data = [['foo','bar','baz'],['my','your'],['holy','grail']]
result = []
for l in data:
     result += l

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Bethard  
View profile  
 More options Dec 13 2004, 4:09 pm
Newsgroups: comp.lang.python
From: Steven Bethard <steven.beth...@gmail.com>
Date: Mon, 13 Dec 2004 21:09:00 GMT
Subject: Re: do you master list comprehensions?

Will Stuyvesant wrote:
>>>>data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>>>result = []
>>>>for d in data:

> ...     for w in d:
> ...        result.append(w)

>>>>print result

> ['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']

Take advantage of the fact that you can have more than one 'for' in a
list comprehension:

 >>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
 >>> [item for item_list in data for item in item_list]
['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']

Steve


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Dec 13 2004, 4:08 pm
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Mon, 13 Dec 2004 22:08:32 +0100
Local: Mon, Dec 13 2004 4:08 pm
Subject: Re: do you master list comprehensions?

Will Stuyvesant wrote:
>>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>>> result = []
>>>> for d in data:
> ...     for w in d:
> ...        result.append(w)
>>>> print result
> ['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']

> puts all the words in a list, like I want.

> How to do this with [lc] instead of for-loops?
>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>> [w for d in data for w in d]

['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']

See how the for expressions in the list comprehension exactly match your
nested for loops? That's all there is to it.

Peter


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Diez B. Roggisch  
View profile  
 More options Dec 13 2004, 4:20 pm
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <deetsNOS...@web.de>
Date: Mon, 13 Dec 2004 22:20:12 +0100
Local: Mon, Dec 13 2004 4:20 pm
Subject: Re: do you master list comprehensions?
>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>> [e for l in data for e in l]

['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']
--
Regards,

Diez B. Roggisch


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fredrik Lundh  
View profile  
 More options Dec 13 2004, 4:48 pm
Newsgroups: comp.lang.python
From: "Fredrik Lundh" <fred...@pythonware.com>
Date: Mon, 13 Dec 2004 22:48:53 +0100
Local: Mon, Dec 13 2004 4:48 pm
Subject: Re: do you master list comprehensions?

Max M wrote:
>> I tried funnies like [[w for w in L] for L in data],
>> that is correct syntax, but you'd never guess.

> That is absolutely correct. It's not a funnie at all. If you find it odd it's only because you are
> not used to list comprehensiones.

well, syntactically correct or not, it doesn't do what he want...

> In that case you might be more comfortable with:

> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
> result = []
> for l in data:
>     result += l

how about (slightly evil):

    result = []; map(result.extend, data)

</F>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Max M  
View profile  
 More options Dec 13 2004, 6:41 pm
Newsgroups: comp.lang.python
From: Max M <m...@mxm.dk>
Date: Tue, 14 Dec 2004 00:41:36 +0100
Local: Mon, Dec 13 2004 6:41 pm
Subject: Re: do you master list comprehensions?

Fredrik Lundh wrote:
> Max M wrote:

>>>I tried funnies like [[w for w in L] for L in data],

>>That is absolutely correct. It's not a funnie at all.

> well, syntactically correct or not, it doesn't do what he want...

Doh! *I* might not be used to list comprehensions then... You are right.

That example could have been expressed more clearly as:

     result = data

;-)

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Luis M. Gonzalez  
View profile  
 More options Dec 13 2004, 8:44 pm
Newsgroups: comp.lang.python
From: "Luis M. Gonzalez" <luis...@gmail.com>
Date: 13 Dec 2004 17:44:19 -0800
Local: Mon, Dec 13 2004 8:44 pm
Subject: Re: do you master list comprehensions?
I guess the simplest to do it is like this:

>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>> result=[w for d in data for w in d]
>>> result

['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Luis M. Gonzalez  
View profile  
 More options Dec 13 2004, 8:44 pm
Newsgroups: comp.lang.python
From: "Luis M. Gonzalez" <luis...@gmail.com>
Date: 13 Dec 2004 17:44:33 -0800
Local: Mon, Dec 13 2004 8:44 pm
Subject: Re: do you master list comprehensions?
I guess the simplest way to do it is like this:

>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>> result=[w for d in data for w in d]
>>> result

['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Stroud  
View profile  
 More options Dec 13 2004, 9:13 pm
Newsgroups: comp.lang.python
From: James Stroud <jstr...@mbi.ucla.edu>
Date: Mon, 13 Dec 2004 18:13:16 -0800
Local: Mon, Dec 13 2004 9:13 pm
Subject: Re: do you master list comprehensions?
Here is one for arbitrary depth:

def unroll(ary):
  unrolled = []
  for item in ary:
    # add test for your favorite sequence type
    if ( type(item) == types.ListType or   \
         type(item) == types.TupleType     \
       ):
      unrolled.extend(unroll(item))
    else:
      unrolled.append(item)
  return unrolled

>>> unroll([[1, 2, 3], ('fred', 'barney', ['wilma', 'betty']), 'dino'])

[1, 2, 3, 'fred', 'barney', 'wilma', 'betty', 'dino']

On Monday 13 December 2004 12:51 pm, Will Stuyvesant wrote:

--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
611 Charles E. Young Dr. S.
MBI 205, UCLA 951570
Los Angeles CA 90095-1570
http://www.jamesstroud.com/

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Bowers  
View profile  
 More options Dec 14 2004, 12:30 am
Newsgroups: comp.lang.python
From: Jeremy Bowers <j...@jerf.org>
Date: Tue, 14 Dec 2004 00:30:43 -0500
Local: Tues, Dec 14 2004 12:30 am
Subject: Re: do you master list comprehensions?

result = data[:]

:-)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fredrik Lundh  
View profile  
 More options Dec 14 2004, 2:44 am
Newsgroups: comp.lang.python
From: "Fredrik Lundh" <fred...@pythonware.com>
Date: Tue, 14 Dec 2004 08:44:10 +0100
Local: Tues, Dec 14 2004 2:44 am
Subject: Re: do you master list comprehensions?

or, shorter:

>>> from Tkinter import _flatten as unroll
>>> (1, 2, 3, 'fred', 'wilma', 'betty', 'dino')

(alright, it returns a tuple, but that can be easily fixed, if necessary)

</F>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Timothy Babytch  
View profile  
 More options Dec 14 2004, 5:49 am
Newsgroups: comp.lang.python
From: Timothy Babytch <t...@zeos.net>
Date: Tue, 14 Dec 2004 12:49:59 +0200
Local: Tues, Dec 14 2004 5:49 am
Subject: Re: do you master list comprehensions?

Will Stuyvesant wrote:
>>>>data = [['foo','bar','baz'],['my','your'],['holy','grail']]

sum(data, [])

['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']

The second parameter passed to sum is just to overrride default
initial value "zero".

--
Timothy Babytch


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Bethard  
View profile  
 More options Dec 14 2004, 12:21 pm
Newsgroups: comp.lang.python
From: Steven Bethard <steven.beth...@gmail.com>
Date: Tue, 14 Dec 2004 17:21:16 GMT
Local: Tues, Dec 14 2004 12:21 pm
Subject: Re: do you master list comprehensions?

Timothy Babytch wrote:
> Will Stuyvesant wrote:

>>>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]

> sum(data, [])

> ['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']

> The second parameter passed to sum is just to overrride default
> initial value "zero".

It's worth keeping in mind that this solution has the same efficiency
problems that a loop that =+ strings does:

 > python -m timeit -s "data = [range(10) for _ in range(100)]"
"sum(data, [])"
1000 loops, best of 3: 530 usec per loop

 > python -m timeit -s "data = [range(10) for _ in range(100)]" "[w for
d in data for w in d]"
10000 loops, best of 3: 151 usec per loop

 > python -m timeit -s "data = [range(10) for _ in range(1000)]"
"sum(data, [])"
10 loops, best of 3: 54.2 msec per loop

 > python -m timeit -s "data = [range(10) for _ in range(1000)]" "[w for
d in data for w in d]"
100 loops, best of 3: 1.75 msec per loop

The sum function used in this way (or a loop with a +=) is O(N**2) while
the LC is O(N).

Steve


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fredrik Lundh  
View profile  
 More options Dec 14 2004, 12:41 pm
Newsgroups: comp.lang.python
From: "Fredrik Lundh" <fred...@pythonware.com>
Date: Tue, 14 Dec 2004 18:41:23 +0100
Local: Tues, Dec 14 2004 12:41 pm
Subject: Re: do you master list comprehensions?

Steven Bethard wrote:
> > python -m timeit -s "data = [range(10) for _ in range(1000)]"
> "sum(data, [])"
> 10 loops, best of 3: 54.2 msec per loop

> > python -m timeit -s "data = [range(10) for _ in range(1000)]" "[w for
> d in data for w in d]"
> 100 loops, best of 3: 1.75 msec per loop

> The sum function used in this way (or a loop with a +=) is O(N**2) while the LC is O(N).

also:

timeit -s "data = [range(10) for _ in range(1000)]" "L = sum(data, [])"
10 loops, best of 3: 4.02e+004 usec per loop

timeit -s "data = [range(10) for _ in range(1000)]" "L = [w for d in data for w in d]"
1000 loops, best of 3: 1.12e+003 usec per loop

timeit -s "data = [range(10) for _ in range(1000)]" "L = []; map(L.extend, data)"
1000 loops, best of 3: 283 usec per loop

timeit -s "data = [range(10) for _ in range(1000)]; from Tkinter import _flatten" "L =
_flatten(data)"
1000 loops, best of 3: 308 usec per loop

(the last one supports arbitrary nestings, the others don't)

</F>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Coghlan  
View profile  
 More options Dec 15 2004, 7:21 am
Newsgroups: comp.lang.python
From: Nick Coghlan <ncogh...@iinet.net.au>
Date: Wed, 15 Dec 2004 22:21:53 +1000
Local: Wed, Dec 15 2004 7:21 am
Subject: Re: do you master list comprehensions?

There's a way to avoid generating the intermediate list if you don't actually
need it (e.g. you want to feed the sequence to another method):

.>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
.>>> from itertools import chain
.>>> result = "".join(chain(*data))
'foobarbazmyyourholygrail'

Some timing with integers:

C:\>python -m timeit -s "data = [range(x) for x in range(1000)]" "L= []; map(L.e
xtend, data); max(L)"
10 loops, best of 3: 78.5 msec per loop

C:\>python -m timeit -s "data = [range(x) for x in range(1000)]; from Tkinter im
port _flatten" "max(_flatten(data))"
10 loops, best of 3: 58.4 msec per loop

C:\>python -m timeit -s "data = [range(x) for x in range(1000)]; from itertools
import chain" "max(chain(*data))"
10 loops, best of 3: 43 msec per loop

And with strings:

C:\>python -m timeit -s "data = [map(str, range(x)) for x in range(1000)]" "L= [
]; map(L.extend, data); ''.join(L)"
10 loops, best of 3: 106 msec per loop

C:\>python -m timeit -s "data = [map(str, range(x)) for x in range(1000)]; from
Tkinter import _flatten" "''.join(_flatten(data))"
10 loops, best of 3: 85.4 msec per loop

C:\>python -m timeit -s "data = [map(str, range(x)) for x in range(1000)]; from
itertools import chain" "''.join(chain(*data))"
10 loops, best of 3: 1.2 sec per loop ****** OUCH!!

C:\>python -m timeit -s "data = [map(str, range(x)) for x in range(1000)]; from
itertools import chain" "''.join(list(chain(*data)))"
10 loops, best of 3: 107 msec per loop

Yikes - looks like chain() really sucks for str.join. However, the addition of
the 'list' call makes a big difference. Maybe PySequence_Fast should be called
PySequence_SlowAsADeadDingo() in this case ;)

(FYI, I filed bug report #1085744 on SF about this)

Cheers,
Nick.

--
Nick Coghlan   |   ncogh...@email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matthew Moss  
View profile  
 More options Dec 15 2004, 2:07 pm
Newsgroups: comp.lang.python
From: "Matthew Moss" <matthew.m...@gmail.com>
Date: 15 Dec 2004 11:07:08 -0800
Local: Wed, Dec 15 2004 2:07 pm
Subject: Re: do you master list comprehensions?

Diez B. Roggisch wrote:
> >>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
> >>> [e for l in data for e in l]
> ['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']

Okay, I tried this in an interactive Python session and it works as
stated. My question is, why?  How is the interpreter parsing that list
expression that it makes sense?

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fredrik Lundh  
View profile  
 More options Dec 15 2004, 2:27 pm
Newsgroups: comp.lang.python
From: "Fredrik Lundh" <fred...@pythonware.com>
Date: Wed, 15 Dec 2004 20:27:20 +0100
Local: Wed, Dec 15 2004 2:27 pm
Subject: Re: do you master list comprehensions?

Matthew Moss wrote:
>> >>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>> >>> [e for l in data for e in l]
>> ['foo', 'bar', 'baz', 'my', 'your', 'holy', 'grail']

> Okay, I tried this in an interactive Python session and it works as
> stated. My question is, why?  How is the interpreter parsing that list
> expression that it makes sense?

the language reference has the answer:

    http://www.python.org/doc/2.3.4/ref/lists.html

    "... the elements of the new list are those that would be produced
    by considering each of the 'for' or 'if' clauses a block, nesting
    from left to right, and evaluating the expression to produce a list
    element each time the innermost block is reached."

or, conceptually, the compiler strips off the expression, and then
inserts newlines, colons and indentation, and wraps the expression
in an append statement.

so

    [e for l in data for e in l]

behaves like:

    result = []
    for l in data:
        for e in l:
            result.append(e)

except that it's an expression, and the result variable is hidden. (and
the compiler and the runtime is of course free to implement this in a
more efficient way)

</F>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Will Stuyvesant  
View profile  
 More options Dec 14 2004, 8:56 am
Newsgroups: comp.lang.python
From: "Will Stuyvesant" <hw...@hotmail.com>
Date: 14 Dec 2004 05:56:18 -0800
Local: Tues, Dec 14 2004 8:56 am
Subject: Re: do you master list comprehensions?
Okay that was fun.  Enlightening as I hoped.  unroll() in Python, for
arbitrary depth, _flatten in Tkinter (what else is in Tkinter!), sum()
abuse.

The sum(data,[]) was funniest, it works like ((['foo','bar'] + []) +
['my','your']) + ['holy','grail'].  Before I think of such things I
have already coded an algorithm in imperative style.  Guess I have not
been exposed to functional programming enough.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "".>>>" is a good idea! (OT, was: Re: do you master list comprehensions?)" by Stefan Behnel
Stefan Behnel  
View profile  
 More options Dec 16 2004, 5:30 am
Newsgroups: comp.lang.python
From: Stefan Behnel <behnel...@gkec.informatik.tu-darmstadt.de>
Date: Thu, 16 Dec 2004 11:30:16 +0100
Local: Thurs, Dec 16 2004 5:30 am
Subject: ".>>>" is a good idea! (OT, was: Re: do you master list comprehensions?)

Nick Coghlan schrieb:

>>>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
>>>>> result = []
>>>>> for d in data:

> .>>> data = [['foo','bar','baz'],['my','your'],['holy','grail']]
> .>>> from itertools import chain
> .>>> result = "".join(chain(*data))
> 'foobarbazmyyourholygrail'

This is the first time I see that and I totally like the idea of writing
".>>>" instead of ">>>" at the beginning of a line. Thank you Dr. Dobb!
It's unfortunate for c.l.py that Python uses ">>>" as the default prompt
as it messes up the display on mail/news readers that provide "syntax
highlighting" for quotes.

I wish everyone would write examples that way! On the other hand - three
levels of quoting are really rare, maybe it would be easier to change that
in the mail readers...

... or in Py3k ?

Stefan


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roel Schroeven  
View profile  
 More options Dec 16 2004, 7:35 am
Newsgroups: comp.lang.python
From: Roel Schroeven <rschroev_nospam...@fastmail.fm>
Date: Thu, 16 Dec 2004 12:35:25 GMT
Local: Thurs, Dec 16 2004 7:35 am
Subject: Re: ".>>>" is a good idea! (OT, was: Re: do you master list comprehensions?)

Off topic, but indeed: I use Quote Colors in Mozilla which is very nice
for reading mails or news posts with quotes, but it's very confusing
with Python's prompt.

Prepending every line with . is not an ideal solution though... I think
it gets tiresome very quickly.

--
"Codito ergo sum"
Roel Schroeven


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Coghlan  
View profile  
 More options Dec 16 2004, 9:22 am
Newsgroups: comp.lang.python
From: Nick Coghlan <ncogh...@iinet.net.au>
Date: Fri, 17 Dec 2004 00:22:19 +1000
Local: Thurs, Dec 16 2004 9:22 am
Subject: Re: ".>>>" is a good idea! (OT, was: Re: do you master list comprehensions?)

Roel Schroeven wrote:
> Stefan Behnel wrote:
>> This is the first time I see that and I totally like the idea of
>> writing ".>>>" instead of ">>>" at the beginning of a line. Thank you
>> Dr. Dobb! It's unfortunate for c.l.py that Python uses ">>>" as the
>> default prompt as it messes up the display on mail/news readers that
>> provide "syntax highlighting" for quotes.

I use Thunderbird, and started doing it so I could read my own posts. I did copy
it from someone, though (but I can't recall who).

The trick can also be useful for web tools that strip leading whitespace.

> Prepending every line with . is not an ideal solution though... I think
> it gets tiresome very quickly.

Aye, can't argue with that. It does have the virtues of reliability and
portability, though :)

Cheers,
Nick.

--
Nick Coghlan   |   ncogh...@email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "do you master list comprehensions?" by Nick Coghlan
Nick Coghlan  
View profile  
 More options Dec 16 2004, 9:30 am
Newsgroups: comp.lang.python
From: Nick Coghlan <ncogh...@iinet.net.au>
Date: Fri, 17 Dec 2004 00:30:46 +1000
Local: Thurs, Dec 16 2004 9:30 am
Subject: Re: do you master list comprehensions?

Nick Coghlan wrote:
> (FYI, I filed bug report #1085744 on SF about this)

And Raymond Hettinger was able to decipher my somewhat incoherent rambling (tip:
don't try to write bug reports in the wee hours of the morning) and produce a
potentially useful modification to PySequence_Tuple.

Anyway, I guess the results I got emphasizes the fact that it's important to
measure performance of the actual methods you're using on representative input
data, rather than relying on overly simple demonstrators.

And, of course, don't be *too* concerned about optimisations until you know you
actually have a performance problem. . .

Cheers,
Nick.

--
Nick Coghlan   |   ncogh...@email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "".>>>" is a good idea! (OT, was: Re: do you master list comprehensions?)" by Steve Holden
Steve Holden  
View profile  
 More options Dec 16 2004, 12:00 pm
Newsgroups: comp.lang.python
From: Steve Holden <st...@holdenweb.com>
Date: Thu, 16 Dec 2004 12:00:34 -0500
Local: Thurs, Dec 16 2004 12:00 pm
Subject: Re: ".>>>" is a good idea! (OT, was: Re: do you master list comprehensions?)

$ python
Python 2.4 (#1, Dec  4 2004, 20:10:33)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> import sys
 >>> sys.ps1 = ".>>> "; sys.ps2 = ".... "
.>>> print """\
.... It isn't that hard"""
It isn't that hard
.>>>

Would it work, I wonder, with a leading space on the prompt? That might
be a valid change to the standard prompt. Let's see

  >>> This line isn't really quoted three times.

regards
  Steve
--
Steve Holden               http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC      +1 703 861 4237  +1 800 494 3119


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Dart  
View profile  
 More options Dec 16 2004, 1:15 pm
Newsgroups: comp.lang.python
From: Keith Dart <kd...@kdart.com>
Date: Thu, 16 Dec 2004 18:15:43 GMT
Local: Thurs, Dec 16 2004 1:15 pm
Subject: Re: ".>>>" is a good idea! (OT, was: Re: do you master list comprehensions?)

What I do is set Python's sys.ps1 variable to something else. I have a
module called "interactive" that I import implicitly by shell alias:

py='python -i -c '\''import interactive'\'

Which, among other things, sets the prompt to "Python> "

433 $ py
Python> print "This has no leader that screws up email programs."
This has no leader that screws up email programs.
Python>

--
                            \/ \/
                            (O O)
-- --------------------oOOo~(_)~oOOo----------------------------------------
Keith Dart <kd...@kdart.com>
public key: ID: F3D288E4
=========================================================================== =


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 38   Newer >
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google