nonunique string replacements
Shashwat Anand
anand.shashwat at gmail.com
Wed Mar 3 02:36:13 EST 2010
>>> s = 'PBUSH 201005 K 500 1. 1. 1.
1. 1.' #Your original string here
>>> l #Create your desired list
['500.2', '5.2', '5.3', '5.4', '5.5', '5.6', '5.7']
>>> lsep = s.count(' 1.') #Count the occurrence of seperators
>>> for i in range(lsep): #Replace all of them by values in list l, one
by one
... f = s.find(' 1.')
... s = s[:f] + l[i] + s[f+3:]
...
>>> s #Your final string
'PBUSH 201005 K 500 500.2 5.2 5.3 5.4
5.5'
~l0nwlf
On Wed, Mar 3, 2010 at 12:36 PM, Ben Racine <i3enhamin at gmail.com> wrote:
> All,
>
> Say I have a string "l" ...
>
> l = 'PBUSH 201005 K 1. 1. 1. 1. 1.
> 1.'
>
> And I want to replace the first " 1." with a "500.2" and the second "
> 1." with " 5.2" ...
>
> What pythonic means would you all recommend?
>
> Note the whitespace is equal between the existing substring and the
> replacement string.
>
> Many thanks,
> Ben Racine
> i3enhamin at gmail.com
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100303/c0ffe31f/attachment-0001.html>
More information about the Python-list
mailing list