I am a newbie to Python and trying out different ways to optimize and simplify my code.
I have a list of arrays(necessarily in this format) initially empty, which I need to update with arrays, making sure that duplicate entries are not added.
Right now I am doing it the following way, which is the only thing i tried out which works:
if len(where(((array(self.pop_next)-(self.pop[self.top_indv_indx[i]]))==0).sum(1)==len((self.pop[self.top_indv_indx[i]])))[0])<=0):
self.pop_next.append(self.pop[self.top_indv_indx[i]])
where self.pop_next is my list of arrays and self.pop[self.top_indv_indx[i]] is the array to be added.
I know this Unpythonic and guess that there are much better simple ways to do the same.
Please Help
You may want to try with
numpy.all(array1 == array2)as condition for an individual array comparison.Extension in edit:
To loop over the list, you may use the following:
This compares
array_to_addto all elements ofarray_listby value. Note thatallhere is__builtin__.all, in contrast tonumpy.all. If you didfrom numpy import *before, this will not work. Useimport numpyinstead and call functions by full name as in the example above.If it is ok to compare by object (i.e. two arrays are only the same if the are the exact same object in memory), use the following simpler variant: