list comprehension

Christopher Barker Chris.Barker at noaa.gov
Thu May 13 14:14:55 EDT 2004


David MacQuigg wrote:
> <guy at NOSPAM.r-e-d.co.nz> wrote:
>>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"

if you can count on there being no spaces between the x,y pairs, this works:

" ".join([ "%s,%i"%(y[0], -int(y[1]) ) for y in  [x.split(",") for x in 
s.split()] ])

Though I don't think I'd do it as a one liner.

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer
                                     		
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the Python-list mailing list