do you master list comprehensions?

R

Roel Schroeven

Stefan said:
Nick said:
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.

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.
 
N

Nick Coghlan

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.
 
N

Nick Coghlan

Nick said:
(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.
 
S

Steve Holden

Nick said:
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.



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

Cheers,
Nick.
$ 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...>>> 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

regards
Steve
 
K

Keith Dart

Steve said:
Nick said:
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.




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

Cheers,
Nick.
$ 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..>>> 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

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 <[email protected]>
public key: ID: F3D288E4
============================================================================
 
R

Roel Schroeven

Steve said:
Nick Coghlan wrote:
$ 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..>>> 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

Seems to work :) Now let's hope lots of people will use an approach like
that for code sent to the various Python newsgroups and mailing lists.
 
K

Kent Johnson

Keith said:
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> "

You can do the same thing using a PYTHONSTARTUP file - see
http://docs.python.org/tut/node4.html#SECTION004240000000000000000

You can change the prompts with
import sys
sys.ps1 = ' >>> '
sys.ps2 = ' ... '

Kent
 
S

Steven Bethard

Kent said:
You can do the same thing using a PYTHONSTARTUP file - see
http://docs.python.org/tut/node4.html#SECTION004240000000000000000

You can change the prompts with
import sys
sys.ps1 = ' >>> '
sys.ps2 = ' ... '

Very cool. I didn't know about this. Does anyone know how to make it
work with Pythonwin[1]? (Obviously, I can type the above in manually
every time, but I'd much rather have Pythonwin do this automatically for
me.)

Steve

[1] I'd do my example code at the command prompt, but I can't live
without copy-paste. ;)
 
F

Fernando Perez

Kent said:
You can do the same thing using a PYTHONSTARTUP file - see
http://docs.python.org/tut/node4.html#SECTION004240000000000000000

You can change the prompts with
import sys
sys.ps1 = ' >>> '
sys.ps2 = ' ... '

<blatant plug>

You might want to look at ipython:

http://ipython.scipy.org,

which provides you automatically with these prompts:

In [1]: for i in range(2):
...: print i,
...:
0 1

In [2]: 99*2
Out[2]: 198

In [3]: _2+1
Out[3]: 199

As a curiosity, ipython was actually born as a sys.ps1/2 hack, by assigning to
these objects whose __repr__ would give numbered prompts with results caching.
These days it's a full-blown pure python interpreter, not a $PYTHONSTARTUP
customization anymore, but it's an interesting little historical curiosity.
Especially if you start going very far with interactive customizations, you
might not want to rewrite all of ipython's 15K lines of code :)

</blatant plug>

Cheers,

f
 
K

Keith Dart

Fernando said:
<blatant plug>

You might want to look at ipython:

http://ipython.scipy.org,


</blatant plug>

I did just recently install that. It looks very nice. Would make a great
interactive prompt for an IDE, as well.



--
\/ \/
(O O)
-- --------------------oOOo~(_)~oOOo----------------------------------------
Keith Dart <[email protected]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4 URL: <http://www.kdart.com/~kdart/public.key>
============================================================================
 
K

Keith Dart

Fernando said:
<blatant plug>

You might want to look at ipython:

http://ipython.scipy.org,


</blatant plug>

I did just recently install that. It looks very nice. Would make a great
interactive prompt for an IDE, as well.



--
\/ \/
(O O)
-- --------------------oOOo~(_)~oOOo----------------------------------------
Keith Dart <[email protected]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4 URL: <http://www.kdart.com/~kdart/public.key>
============================================================================
 
A

aleks90210

Thought you might enjoy my super-small flatten function: (though google
groups seems to be munging my whitespace today)

def flatten(d):
"flatten([[[1,[2,3],[4,5]],6],[7]])==[1,2,3,4,5,6,7]"
return reduce(lambda a,b:a+b,[(type(x) in (list, tuple) \
and flatten(x) or [x]) for x in d])
 
K

Kent Johnson

Steven said:
Very cool. I didn't know about this. Does anyone know how to make it
work with Pythonwin[1]? (Obviously, I can type the above in manually
every time, but I'd much rather have Pythonwin do this automatically for
me.)

Steve

[1] I'd do my example code at the command prompt, but I can't live
without copy-paste. ;)

You can copy and paste from a Windows command prompt. It's a bit bizarre, but
- In the system menu for a command window, pick Properties
- On the Options tab, turn on Quick Edit mode
- Now you can copy and paste with right-click (!). If you have text selected, right-click will copy,
otherwise paste. It's a bit strange but it works.

I think it's wonderfully ironic that in Windows - which takes such pains to make everything keyboard
accessible - in a *command line* window, which is using keyboard input by its nature - you have to
use the mouse for copy and paste!!

Kent
 
S

Steven Bethard

Kent said:
You can copy and paste from a Windows command prompt. It's a bit
bizarre, but
- In the system menu for a command window, pick Properties
- On the Options tab, turn on Quick Edit mode
- Now you can copy and paste with right-click (!). If you have text
selected, right-click will copy, otherwise paste. It's a bit strange but
it works.

Yeah, I'm familiar with this -- all my command windows are enabled with
Quick Edit. =) The problem is that I can't then edit what I've
copy-pasted. I can, of course, copy a few lines, type in an edited
line, copy a few more lines, type in another edited line, etc., but this
gets tedious...

Sorry about not being clear about my problem!

STeve
 
F

Fernando Perez

Keith said:
I did just recently install that. It looks very nice. Would make a great
interactive prompt for an IDE, as well.

Glad you like it :) And yes, there are plans to clean it up so it can be
easily embedded into a full-blown IDE (as well as remaining available for
regular command-line use). This keeps on getting delayed by inevitable bug
fixes and my very limited time, but it will happen...

Best,

f
 
B

Bengt Richter

Steven said:
Very cool. I didn't know about this. Does anyone know how to make it
work with Pythonwin[1]? (Obviously, I can type the above in manually
every time, but I'd much rather have Pythonwin do this automatically for
me.)

Steve

[1] I'd do my example code at the command prompt, but I can't live
without copy-paste. ;)

You can copy and paste from a Windows command prompt. It's a bit bizarre, but
- In the system menu for a command window, pick Properties
- On the Options tab, turn on Quick Edit mode
- Now you can copy and paste with right-click (!). If you have text selected, right-click will copy,
otherwise paste. It's a bit strange but it works.

I think it's wonderfully ironic that in Windows - which takes such pains to make everything keyboard
accessible - in a *command line* window, which is using keyboard input by its nature - you have to
use the mouse for copy and paste!!
Not true on NT4 at least:

Alt-Spacebar gets you the system menu
Follow with e then k which gets you in select mode

use arrow keys to move to top left of a box, or whole screen
(if there's more buffer than screen, arrows should cause scrolling
to access more)

hold shift key down and use arrow keys while continuing to hold
the shift key down to select a box of text (need not be at edges)
this can also scroll through buffer as necessary to select more than can
be visible at one time. Just keep holding down shift.

press enter to capture box of text to clipboard

paste as usual with ctrl-v ins some apps, shift-insert in vim (depending),
or paste back into the dos window with Alt-spacebar e p

Regards,
Bengt Richter
 
S

Scott David Daniels

Bengt said:
Not true on NT4 at least:
Alt-Spacebar gets you the system menu ....
paste into the dos window with Alt-spacebar e p
Thanks immensely for this -- I love it.

--Scott David Daniels
(e-mail address removed)
 
S

Steven Bethard

I said:
Kent said:
You can do the same thing using a PYTHONSTARTUP file - see
http://docs.python.org/tut/node4.html#SECTION004240000000000000000

You can change the prompts with
import sys
sys.ps1 = ' >>> '
sys.ps2 = ' ... '


Very cool. I didn't know about this. Does anyone know how to make it
work with Pythonwin[1]?

Solved my own problem here. For anyone else out there using PythonWin,
the solution that worked for me is to put the following (or whatever
customizations you want) in a sitecustomize.py somewhere on my PYTHONPATH:

import sys
sys.ps1 = 'py> '
sys.ps2 = '... '

PythonWin (as well as my Command Prompt python) now has the above prompts.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,214
Messages
2,571,112
Members
47,706
Latest member
HFWTina07

Latest Threads

Top