list comprehension

Ryan Paul segphault at sbcglobal.net
Mon May 10 03:12:55 EDT 2004


On Mon, 10 May 2004 12:32:20 +1200, Guy Robinson wrote:

> Hello,
> 
> Trying to change a string(x,y values) such as :
> 
> s = "114320,69808 114272,69920 113568,71600 113328,72272"
> 
> into (x,-y):
> 
> out = "114320,-69808 114272,-69920 113568,-71600 113328,-72272"
> 
> I tried this:
> 
> print [(a[0],-a[1] for a in x.split(',')) for x in e]
> 
> But it doesn't work. Can anyone suggest why or suggest an alternative 
> way? The text strings are significantly bigger than this so performance 
> is important.
> 
> TIA,
> 
> Guy

this works:

s = "114320,69808 114272,69920 113568,71600 113328,72272"

o = [(int(a[0]),-int(a[1])) for a in [b.split(',') for b in s.split(' ')]]
print o

[(114320, -69808), (114272, -69920), (113568, -71600), (113328, -72272)]




More information about the Python-list mailing list