Cleaning up conditionals

Deborah Swanson python at deborahswanson.net
Mon Jan 2 05:37:19 EST 2017


Jussi Piitulainen wrote, on January 02, 2017 1:44 AM
> 
> Deborah Swanson writes:
> > Jussi Piitulainen wrote:
> 
> [snip]
> 
> >> With your particular conditions of non-emptiness, which is 
> taken to 
> >> be truth, you can achieve variations of this result with 
> any of the 
> >> following statements:
> >> 
> >> w = ( l1[v] if len(l1[v]) > 0 else
> >>       l2[v] if len(l2[v]) > 0 else
> >>       l1[v] )
> >> 
> >> x = l1[v] if l1[v] else l2[v] if l2[v] else l1[v]
> >> 
> >> y = l1[v] or l2[v] or l1[v]
> >> 
> >> z = l1[v] or l2[v]
> >> 
> >> The last one, which I originally suggested (and still prefer
> >> when otherwise appropriate), is subtly different from the 
> >> others. That difference should be irrelevant.
> >
> > I agree, if the goal was to capture one of the field values in a 
> > scalar value.
> 
> To store into a list, specify a position in the list as the target.

Aha. Now I see your plan. Pardon me, but the single letter variable you
were assigning to, with no prior definition, seemed to me to be a
scalar. I just couldn't see how that could work. But a list,
specifically a list of the 2 rows with identical fields except for the
date, makes perfect sense.

> My idea here has been to simply do this to all the relevant 
> positions in both lists, even when it means storing the old 
> value back.

Not a problem, since it's the same value.

> See below, concretely, with your two examples and the mixed 
> one from Dennis Lee Bieber, where I introduced a small 
> difference of my own so that corresponding non-empty fields 
> differ. I have made it output Python comments and inserted 
> them at appropriate places.
> 
> The same function, merge, fills the empty fields from the 
> other list in all three cases using the method z from above. 
> It does no harm when a field is already non-empty.
> 
> def merge(l1, l2):
>     fields = range(5)
>     for v in fields:
>         l1[v] = l1[v] or l2[v]
>         l2[v] = l2[v] or l1[v]
> 
> l1 = [ '2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, w/d' ]
> l2 = [ '2 br, Elk Plains', '12-29', '',      '',      ''            ]
> 
> print('# Before:', l1, l2, sep = '\n# ', end = '\n# ') 
> merge(l1, l2) print('After:', l1, l2, sep = '\n# ', end = '\n\n')
> 
> # Before:
> # ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, 
> w/d'] # ['2 br, Elk Plains', '12-29', '', '', ''] # After: # 
> ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, 
> w/d'] # ['2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, w/d']
> 
> l1 = [ '2 br, Elk Plains', '12-26', '',      '',      ''            ]
> l2 = [ '2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, w/d' ]
> 
> print('# Before:', l1, l2, sep = '\n# ', end = '\n# ') 
> merge(l1, l2) print('After:', l1, l2, sep = '\n# ', end = '\n\n')
> 
> # Before:
> # ['2 br, Elk Plains', '12-26', '', '', '']
> # ['2 br, Elk Plains', '12-29', 'WA/pi', 'house', 'garage, 
> w/d'] # After: # ['2 br, Elk Plains', '12-26', 'WA/pi', 
> 'house', 'garage, w/d'] # ['2 br, Elk Plains', '12-29', 
> 'WA/pi', 'house', 'garage, w/d']
> 
> l1 = [ '2 br, Elk Plains', '12-26', 'WA/pi', '',      ''            ]
> l2 = [ '2 br, Elf Plains', '12-29', '',      'house', 'garage, w/d' ]
> 
> print('# Before:', l1, l2, sep = '\n# ', end = '\n# ') 
> merge(l1, l2) print('After:', l1, l2, sep = '\n# ', end = '\n\n')
> 
> # Before:
> # ['2 br, Elk Plains', '12-26', 'WA/pi', '', '']
> # ['2 br, Elf Plains', '12-29', '', 'house', 'garage, w/d']
> # After:
> # ['2 br, Elk Plains', '12-26', 'WA/pi', 'house', 'garage, 
> w/d'] # ['2 br, Elf Plains', '12-29', 'WA/pi', 'house', 'garage, w/d']

Very clever, and exactly what I need to accomplish. Well, the first part
of it anyway. My second test is if neither of two corresponding fields
is empty and they're different, to dump which fields are different into
a memo field, but perhaps that could be the first test and then use your
merge for the ones that are identical except for the missing data. 

An example of what I mean is:

l1 = [ '2 br, Elk Plains', '12-26', 'WA/pi', 	'']
l2 = [ '2 br, Elf Plains', '12-29', 'OR/co',    'house', 'garage, w/d' ]

which could happen if I didn't know that both Washington and Oregon have
an Elk Plains.
 
It would mean 2 functions, but if I can work in the logistics of dealing
with all the duplicates (except for the date) in one throw, 2 functions
would be worth it. And not a big deal even if I just stick to 2 rows at
a time.

In fact, just looking at the example I made up above, looks like it
would be better to test for fields that are different first, and pass on
merging the rows if any differences are found. They can always be merged
in a later run after I reconcile the descrepancies.

I'll mess around with it tomorrow, but I'll bet this works, and works
better than what I have now.




More information about the Python-list mailing list