clarification
samwyse
dejanews at email.com
Sat Aug 18 11:02:58 EDT 2007
samwyse wrote:
> Scott David Daniels wrote:
>
>> lefts = set()
>> rights = set()
>> with open('sheet1', 'r') as fh:
>> for line in fh:
>> trimmed = line.strip()
>> if trimmed: # Skip blanks (file end often looks like that)
>> left, right = line.strip().split('\t')
>> lefts.add(left)
>> rights.add(right)
>> result = lefts & rights
>>
>> -Scott
>
>
> # change to however many columns may later exist
> cols = [ set() for i in range(0, 2) ]
> with open('sheet1', 'r') as fh:
> for line in fh:
> for col, data in zip(cols, line.strip().split('\t')):
> col.add(data)
> result = cols[0] & cols[1]
My laptop started complaining about low power before I was really
finished with this last night, so I just hit Send and went to bed. The
last line really should be:
result = reduce(operator.and_, cols)
I'll note that using 'zip' transparently deals with handling those cases
where there are either more or fewer columns of data than expected.
Finally, does anyone familar with P3K know how best to do the reduction
without using 'reduce'? Right now, sets don't support the 'add' and
'multiply' operators, so 'sum' and (the currently ficticious) 'product'
won't work at all; while 'any' and 'all' don't work as one might hope.
Are there an 'intersection' and 'union' built-ins anywhere?
More information about the Python-list
mailing list