Object copies...

Mel Wilson mwilson at the-wire.com
Wed Jan 15 09:14:52 EST 2003


In article <6af8e801.0212222321.3bd5b56f at posting.google.com>,
pennedinil at excite.com (DP) wrote:
>Hi all,
>
>Please help me clear my confusion here. I'm running the script below.
>I'm expecting to get a listing of my original object and the new
>object created by 'getdates'. Instead I'm seeing the function
>'getdates' changing my original object, 'mydates'.
>
>Q1: Why does 'getdates' not create a copy of 'mydates' instead of
>operating on the original object? I.e., how do I change this behavior?
>
>Q2: [Unrelated] I'm trying to parse these dates into d, m, y and can't
>think of any other way but on a per-case basis. Any suggestions on a
>better aproach? I tried -
>try:
>	d. m, y = Dates[i].split("/")
>except: ValueError ...<etc>
>but there are too many exceptions to the rule, which brings me back to
>a case-based algorithm.
>
>Thanks in advance.
>Dinil.
>
>
># -----------------------------------------------------------------------
>def indexedList(list):
>	return map(None, range(len(list)), list)
>
>def getdate(Dates):
>	for i, j in indexedList(Dates):
>		j = j.replace(".", "/")
>		j = j.replace("-", "/")
>		j = j.replace(" ", "/")
>		Dates[i] = j

Because you told it to, right here.  'Dates' is
just another name for `mydates` at this point.

My take would be:

def fix_up_date (d):
    d = d.replace ('.', '/')
    d = d.replace ('-', '/')
    d = d.replace (' ', '/')
    return d

newdates = map (fix_up_date, mydates)

or if you prefer:

newdates = [fix_up_date (d) for d in mydates]


Q2: Once the dates are well-formed, the split should work.
The question is where are they coming from, and what will
it take to make them well-formed?


        Regards.        Mel.




More information about the Python-list mailing list