I need to merge and de-duplicate some lists, and I have some code
which works but doesn't seem particularly elegant. I was wondering if
somebody could point me at a cleaner way to do it.
Here's my function:
+++++++++++++++ ++++
from sets import Set
def mergeLists (intialList, sourceOfAdditio nalLists,
nameOfMethodToC all) :
workingSet = Set(initialList )
for s in sourceOfAdditio nalLists :
getList = s.__getAttribut e__(nameOfMetho dToCall)
workingSet = workingSet.unio n(Set \
(callable(getLi st) and getList() or getList))
return workingSet
++++++++++++++
Two questions - passing the *name* of the method to call, and then
looking it up for each object in the list of extra sources (all of
which need to be new-style objects - not a problem in my application)
seems inelegant. My "sourcesOfAddit ionalLists" are normally all of the
same class - is there something I can bind at class level that
automagically refers to instance-level attributes when invoked?
Second (hopefully clearer & less obscure) question : is
sets.Set.union( ) an efficient way to do list de-duplication? Seems
like the obvious tool for the job.
which works but doesn't seem particularly elegant. I was wondering if
somebody could point me at a cleaner way to do it.
Here's my function:
+++++++++++++++ ++++
from sets import Set
def mergeLists (intialList, sourceOfAdditio nalLists,
nameOfMethodToC all) :
workingSet = Set(initialList )
for s in sourceOfAdditio nalLists :
getList = s.__getAttribut e__(nameOfMetho dToCall)
workingSet = workingSet.unio n(Set \
(callable(getLi st) and getList() or getList))
return workingSet
++++++++++++++
Two questions - passing the *name* of the method to call, and then
looking it up for each object in the list of extra sources (all of
which need to be new-style objects - not a problem in my application)
seems inelegant. My "sourcesOfAddit ionalLists" are normally all of the
same class - is there something I can bind at class level that
automagically refers to instance-level attributes when invoked?
Second (hopefully clearer & less obscure) question : is
sets.Set.union( ) an efficient way to do list de-duplication? Seems
like the obvious tool for the job.
Comment