python - Having problems in extracting duplicates -
i stumped problem, , no matter how around it, still giving me same result.
basically, supposedly have 2 groups - grpa_null , grpb_null, each having 2 meshes in them , named same, brick_geo , bars_geo - result: grpa_null --> brick_geo, bars_geo
but reason, in code below presume 1 giving me problems, when run, program states grpa_null has same duplicates grpb_null, referencing brick_geo , bars_geo. code run, children geo have numerical value behind, - result: grpa_null --> brick_geo0, bars_geo0, grpb_null1 --> brick_geo, bars_geo1
and so, tried modify code such long parent (grpa_null , grpb_null) different, shall not 'touch' on children.
could kindly advice me on it?
def extractduplicateboxlist(self, inputs): result = {} in range(0, len(inputs)): print '<<< : %s' %i n in range(0, len(inputs)): print '<<< n %s' %n if != n: name = inputs[i].getshortname() # result: brick_geo lname = inputs[i].getlongname() # result: |grpa_null|concrete_geo if name == inputs[n].getshortname(): # if list created result. if result.has_key(name): # make sure not in list , add it. alreadyadded = false box in result[name]: if box == inputs[i]: alreadyadded = true if alreadyadded == false: result[name].append(inputs[i]) # otherwise create new list , add it. else: result[name] = [] result[name].append(inputs[i]) return result
there couple of things may want aware of. first , foremost, indentation matters in python. don't know if indentation of code is intended, function code should indented further in function def.
secondly, find question little difficult understand. there several things improve code.
in collections module, there (or should be) type called defaultdict. type similar dict, except having default value of type specify. defaultdict(int) have default of 0 when key, if key wasn't there before. allows implementation of counters, such find duplicates without sorting.
from collections import defaultdict counter = defaultdict(int) item in items: counter[item] += 1
this brings me point. python loops implement for-each structure. never need enumerate items in order access them. so, instead of
for in range(0,len(inputs)):
you want use
for input in inputs:
and if need enumerate inputs
for i,input in enumerate(inputs):
finally, can iterate , filter through iterable objects using list comprehensions, dict comprehensions, or generator expressions. powerful. see create dictionary list comprehension in python
try code out, play it. see if works you.
from collections import defaultdict def extractduplicateboxlist(self, inputs): counts = defaultdict(int) input in inputs: counts[input.getshortname()] += 1 dup_shns = set([k k,v in counts.items() if v > 1]) dups = [i in inputs if input.getshortname() in dup_shns] return dups
Comments
Post a Comment