list comprehension

Guy Robinson guy at NOSPAM.r-e-d.co.nz
Sun May 9 21:17:19 EDT 2004


This works I was just wondering if something could be written more 
concisely and hopefully faster:

s = "114320,69808 114272,69920 113568,71600 113328,72272"
e = s.split(' ')
out =''
for d in e:
     d =d.split(',')
     out +='%s,%d ' %(d[0],-int(d[1]))
print out

Guy

Aahz wrote:

> In article <c7mii9$988$1 at lust.ihug.co.nz>,
> Guy Robinson  <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"
>>
>>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.
> 
> 
> When in doubt, break a problem into smaller steps.  Let me play Socrates
> for a minute: what's the first step in working with your dataset?



More information about the Python-list mailing list