Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > escape sequences in list comprehensions

Reply
Thread Tools

escape sequences in list comprehensions

 
 
kartik
Guest
Posts: n/a
 
      11-12-2004
Escape sequences don't seem to work in strings within list comprehensions:
>>> print ['%s\n' %i for i in [1,2,3]]

['1\n', '2\n', '3\n']

What am I missing?

Thank you.
 
Reply With Quote
 
 
 
 
Kent Johnson
Guest
Posts: n/a
 
      11-12-2004
kartik wrote:
> Escape sequences don't seem to work in strings within list comprehensions:
>
>>>>print ['%s\n' %i for i in [1,2,3]]

>
> ['1\n', '2\n', '3\n']
>
> What am I missing?


This is correct; what were you expecting?

When you print a list, it prints the repr() of the list elements. In the
case of strings, it shows you the escaped form of the string. Maybe this
is what you want:
>>> a=['%s\n' %i for i in [1,2,3]]
>>> a

['1\n', '2\n', '3\n']
>>> for x in a:

.... print x
....
1

2

3

>>>


Note the extra lines when the strings are actually printed.

Kent

>
> Thank you.

 
Reply With Quote
 
 
 
 
Russell Blau
Guest
Posts: n/a
 
      11-12-2004
"kartik" <> wrote in message
news: om...
> Escape sequences don't seem to work in strings within list comprehensions:
>>>> print ['%s\n' %i for i in [1,2,3]]

> ['1\n', '2\n', '3\n']
>
> What am I missing?


You're missing what happens when you print *any* list containing strings:

>>> print ['\n', '\n']

['\n', '\n']

--
I don't actually check my hotmail account, but you can replace hotmail with
excite if you really want to contact me.


 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      11-12-2004
kartik wrote:
> Escape sequences don't seem to work in strings within list comprehensions:
>
>>>>print ['%s\n' %i for i in [1,2,3]]

>
> ['1\n', '2\n', '3\n']
>
> What am I missing?


Other have answered to this question.

Just try this instead :
print "\n".join(["%d" % i for i in [1,2,3]])

> Thank you.

HTH

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      11-13-2004
kartik wrote:
> Escape sequences don't seem to work in strings within list comprehensions:
>
>>>>print ['%s\n' %i for i in [1,2,3]]

>
> ['1\n', '2\n', '3\n']
>
> What am I missing?


Others have answered, but not explained. When you print
a list (in other words, when you display the results of
calling str() on a list), the list chooses how to display
itself. It chooses to display the brackets at the start
and end, the commas separating the items, and the results
of calling repr() on each individual item. That means
that strings are shown with quotation marks (which are not
part of the string normally) and with special characters
represented as escape sequences.

The following appears to be what you believed you wanted,
though it's not really what you wanted <wink>:

lst = ['%s\n' % i for i in [1,2,3]]
print '[' + ', '.join([str(x) for x in lst]) + ']'

-Peter
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      11-13-2004
On Fri, 12 Nov 2004 22:46:17 -0500, Peter Hansen <> wrote:

>kartik wrote:
>> Escape sequences don't seem to work in strings within list comprehensions:
>>
>>>>>print ['%s\n' %i for i in [1,2,3]]

>>
>> ['1\n', '2\n', '3\n']
>>
>> What am I missing?

>
>Others have answered, but not explained. When you print
>a list (in other words, when you display the results of
>calling str() on a list), the list chooses how to display
>itself. It chooses to display the brackets at the start
>and end, the commas separating the items, and the results
>of calling repr() on each individual item. That means
>that strings are shown with quotation marks (which are not
>part of the string normally) and with special characters
>represented as escape sequences.
>
>The following appears to be what you believed you wanted,
>though it's not really what you wanted <wink>:
>
>lst = ['%s\n' % i for i in [1,2,3]]
>print '[' + ', '.join([str(x) for x in lst]) + ']'
>

Maybe this is what he wanted (not recommended for general use :

>>> class FunnyStr(str):

... def __repr__(self): return str(self)
...

Plain:
>>> lst = ['%s\n' % i for i in [1,2,3]]
>>> lst

['1\n', '2\n', '3\n']

Funny:
>>> lst = [FunnyStr('%s\n' % i) for i in [1,2,3]]
>>> lst

[1
, 2
, 3
]
>>> print lst

[1
, 2
, 3
]

Regards,
Bengt Richter
 
Reply With Quote
 
Josiah Carlson
Guest
Posts: n/a
 
      11-13-2004

(Bengt Richter) wrote:
>
> On Fri, 12 Nov 2004 22:46:17 -0500, Peter Hansen <> wrote:
>
> >kartik wrote:
> >> Escape sequences don't seem to work in strings within list comprehensions:
> >>
> >>>>>print ['%s\n' %i for i in [1,2,3]]
> >>
> >> ['1\n', '2\n', '3\n']
> >>
> >> What am I missing?

> >
> >Others have answered, but not explained. When you print
> >a list (in other words, when you display the results of
> >calling str() on a list), the list chooses how to display
> >itself. It chooses to display the brackets at the start
> >and end, the commas separating the items, and the results
> >of calling repr() on each individual item. That means
> >that strings are shown with quotation marks (which are not
> >part of the string normally) and with special characters
> >represented as escape sequences.
> >
> >The following appears to be what you believed you wanted,
> >though it's not really what you wanted <wink>:
> >
> >lst = ['%s\n' % i for i in [1,2,3]]
> >print '[' + ', '.join([str(x) for x in lst]) + ']'
> >

> Maybe this is what he wanted (not recommended for general use :
>
> >>> class FunnyStr(str):

> ... def __repr__(self): return str(self)
> ...
>
> Plain:
> >>> lst = ['%s\n' % i for i in [1,2,3]]
> >>> lst

> ['1\n', '2\n', '3\n']
>
> Funny:
> >>> lst = [FunnyStr('%s\n' % i) for i in [1,2,3]]
> >>> lst

> [1
> , 2
> , 3
> ]
> >>> print lst

> [1
> , 2
> , 3
> ]



If that's really what he wants, he should take a look at the pprint
module.

- Josiah

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read strings cantaining escape character from a file and useit as escape sequences? slomo Python 5 12-02-2007 11:39 AM
list(...) and list comprehensions (WAS: Arithmetic sequences in Python) Steven Bethard Python 7 01-20-2006 04:13 PM
vhdl textio and escape sequences Olaf Petzold VHDL 1 11-28-2005 06:22 PM
processing escape sequences exactly like compiler does Harald Kirsch Java 0 11-19-2004 11:34 AM
Escape sequences and printing Kit C Programming 5 09-17-2003 02:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57